Where's the JUnit Matcher # startsWith declaration?

I was looking through junit ExpectedExceptions ' javadoc , and I can't figure out where startsWith is startsWith in their example (marked HERE in the code). I checked the CoreMatcher utility class , but could not find the static startsWith method.

Where is this method located?

(I obviously can write it myself, but it’s not)

 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?"); } } 
+7
source share
3 answers

Most likely, this is the startsWith method from the Hamcrest Matchers class.

+7
source
 import static org.hamcrest.core.StringStartsWith.startsWith; 

allows both

 assertThat(msg, startsWith ("what")); 

and

 ExpectedException.none().expectMessage(startsWith("What")); //HERE 
+5
source

Looking at ExpectedException , we see that there are two expectMessage methods, one String and one Matcher, which is really org.hamcrest.Matcher .

 /** * Adds to the list of requirements for any thrown exception that it should * <em>contain</em> string {@code substring} */ public void expectMessage(String substring) { expectMessage(containsString(substring)); } /** * Adds {@code matcher} to the list of requirements for the message returned * from any thrown exception. */ public void expectMessage(Matcher<String> matcher) { expect(hasMessage(matcher)); } 
+3
source

All Articles