Tuesday, 27 February 2018

Hamcrest: containsString: match sub string

By using 'containsString' method, you can create a matcher that matches if the examined string contains the specified String anywhere.

Ex
String str = "Hello World";
assertThat("str should contain 'World'", str, containsString("World"));

TestApp.java
package com.sample.model;

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

import org.junit.Test;

public class TestApp {

 @Test
 public void containsString_testWithDiffScenarios() {
  String str = "Hello World";

  assertThat("str should contain 'World'", str, containsString("World"));
  assertThat("str should contain 'Hello'", str, containsString("Hello"));
  assertThat("str should contain ' '", str, containsString(" "));

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment