Sto usando JUnit 4.11.Perché l'eccezione JUnit Test fallisce sempre?
Eclipse Versione: Luna Service Release 2 (4.4.2)
ho bisogno di fare il test eccezione. Il codice può essere il seguente:
// src code
public void myMethod throws MyException {
...
throw new MyException(msg);
...
}
// test code
@Test (expected = MyException.class)
public void testMyMethod() {
try {
myMethod();
fail();
} catch (MyException e) {
assertEquals(expectedStr, e.getMessage());
}
}
Ma il test fallisce sempre, dove sto sbaglio?
Aggiunta alla risposta (corretta) di @rgettman: È necessario verificare le eccezioni con JUnit's ['ExpectedException'] (http://junit.org/apidocs/org/junit/rules/ExpectedException.html). – Seelenvirtuose