Thursday, 5 April 2018

Hamcrest: allOf: Match against all the matchers

By using 'allOf' method, you can create a matcher that matches if the specified element is matched against all the matchers.

Ex
String name = "Hello World";
assertThat(name, allOf(startsWith("Hello"), containsString("rld"), endsWith("World")));

Find the below working application.

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  String name = "Hello World";

  assertThat(name, allOf(startsWith("Hello"), containsString("rld"), endsWith("World")));

 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment