2012-10-20 15 views
7

Przeglądałem numer junit ExpectedExceptions' javadoc i nie mogę zrozumieć, skąd pochodzi startsWith w swoim przykładzie (oznaczony TUTAJ w kodzie). Sprawdziłem numer CoreMatcher utility class, ale nie mogłem znaleźć żadnej statycznej metody startsWith.Gdzie zaczyna się deklaracja JUnit Matcher #?

Gdzie znajduje się ta metoda?

(oczywiście mogę napisać to sam, ale to nie o to chodzi)

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

Odpowiedz

3

Patrząc na ExpectedException, widzimy, że są zdefiniowane dwie metody expectMessage, jeden String i jeden Matcher, który jest rzeczywiście 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)); 
} 
5
import static org.hamcrest.core.StringStartsWith.startsWith; 

umożliwia zarówno

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

i

ExpectedException.none().expectMessage(startsWith("What")); //HERE 
Powiązane problemy