Thursday 5 April 2018

Hamcrest: either: match against either of the matcher

By using 'either' method, you can create a matcher that matches when either of the specified matchers match the examined object

Ex
String name = "Hello World";
assertThat(name, either(startsWith("Hello")).or(containsString("abrakadabra")));

Find the below working example.

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String name = "Hello World";
  assertThat(name, either(startsWith("Hello")).or(containsString("abrakadabra")));
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment