By using 'endsWith' method, you can create a matcher that matches if the examined String ends with the specified string.
Ex
assertThat("str should ends with 'd'", str, endsWith("d"));
Find the below working example
TestApp.java
package com.sample.model;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import org.junit.Test;
public class TestApp {
@Test
public void containsString_testWithDiffScenarios() {
String str = "Hello World";
assertThat("str should ends with 'd'", str, endsWith("d"));
assertThat("str should ends with 'ld'", str, endsWith("ld"));
assertThat("str should ends with 'rld'", str, endsWith("rld"));
assertThat("str should ends with 'orld'", str, endsWith("orld"));
assertThat("str should ends with 'World'", str, endsWith("World"));
}
}
Previous Next Home
This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here