Thursday 5 April 2018

Hamcrest: both: Match against two matchers

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

Ex
String name = "Hello World";
assertThat(name, both(startsWith("Hello")).and(containsString("rld")));

TestApp.java
package com.sample.tests;

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




Previous                                                 Next                                                 Home

No comments:

Post a Comment