Thursday, 5 April 2018

Hamcrest: anyOf: match against any one of the matcher

By using 'anyOf' method, you can create a matcher that matches if the examined object matches to ANY of the specified matchers.

Ex
String name = "Hello World";
assertThat(name, anyOf(startsWith("Hello"),containsString("abrakadabra")));

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
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, anyOf(startsWith("Hello"),containsString("abrakadabra")));
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment