9

私はjunitExpectedExceptionsのjavadocを閲覧していましたが、それらの例のがどこから来ているのか理解できませんstartsWith(コードでここにマークされています)。CoreMatcherユーティリティクラスを確認しましたが、静的startsWithメソッドが見つかりませんでした。

そのメソッドはどこにありますか?

(私は明らかにそれを自分で書くことができますが、それは重要ではありません)

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?");
    }
}
4

3 に答える 3

10
import static org.hamcrest.core.StringStartsWith.startsWith;

両方を有効にします

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

ExpectedException.none().expectMessage(startsWith("What")); //HERE
于 2014-02-17T06:38:17.230 に答える
8

おそらく、これはstartsWithHamcrestorg.hamcrest.Matchersクラスのメソッドです。

于 2012-10-20T09:55:34.227 に答える
3

ExpectedExceptionを見ると、2 つの expectMessage メソッドが定義されていることがわかります。1 つは String で、もう 1 つは Matcherorg.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));
}
于 2012-10-20T16:29:28.113 に答える