Search code examples
javajunithamcrest

Where is the declaration of JUnit Matcher#startsWith?


I was browsing the junit ExpectedExceptions' javadoc and I can't understand where the startsWith in their example comes from (marked HERE in the code). I checked the CoreMatcher utility class but could not find any static startsWith method.

Where is that method located?

(I can obviously write it myself but that's not the point)

public static class HasExpectedException {
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void throwsNullPointerExceptionWithMessage() {
        thrown.expect(NullPointerException.class);
        thrown.expectMessage("happened?");
        thrown.expectMessage(startsWith("What")); //HERE
        throw new NullPointerException("What happened?");
    }
}

Solution

  • Most likely this is the startsWith method from the Hamcrest org.hamcrest.Matchers class.