2012-10-20 23 views
7

junit ExpectedExceptions' javadoc'a göz atıyordum ve örneklerinde startsWith'un nereden geldiğini anlamıyorum (kodda BURADA işaretli). CoreMatcher utility class'u kontrol ettim ancak statik startsWith yöntemini bulamadım.JUnit Matcher # opensWith'in beyanı nerede?

Bu yöntem nerede bulunuyor?

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

cevap

7

Büyük olasılıkla bu hamcrest Matchers class den startswith yöntemidir

(Açıkçası bunu kendim yazabilir ama konu bu değil).

3

ExpectedException'a baktığımızda, org.hamcrest.Matcher numaralı, gerçekten bir tane String ve bir Matcher olmak üzere iki bekliyorMessage yönteminin tanımlandığını görebiliriz.

/** 
* 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)); 
} 
5
import static org.hamcrest.core.StringStartsWith.startsWith; 

hem

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

ve

ExpectedException.none().expectMessage(startsWith("What")); //HERE 
sağlar