2012-10-20 16 views
7

Stavo navigando su junit ExpectedExceptions' javadoc e non riesco a capire da dove provenga il codice startsWith (contrassegnato QUI nel codice). Ho controllato lo CoreMatcher utility class ma non ho trovato alcun metodo statico startsWith.Dove viene avviata la dichiarazione di JUnit Matcher #Con?

Dove si trova questo metodo?

(posso ovviamente scrivere io stesso, ma non è questo il punto)

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

risposta

7

Molto probabilmente questo è il metodo startsWith dal Hamcrest Matchers class.

3

Guardando a ExpectedException, possiamo vedere che ci sono due metodi expectMessage definiti, uno String e un Matcher, che è effettivamente 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; 

presenta sia

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

e

ExpectedException.none().expectMessage(startsWith("What")); //HERE 
Problemi correlati