Search code examples
javajunitjunit4

Test fails because of Exception thrown


I have this unit test, that the overall test fails because of the exceptions that is thrown, although its expected:

@Test(expected = AutoGenerateStringIdException.class)
public void testPut_shouldThrowException(){
    RootEntity rootObject = new RootEntity(); 
    // Some codes here
    try {
        Key key = store.put(rootObject);
    } catch(AutoGenerateStringIdException e){
        assertEquals(e.getMessage(), "Cannot auto-generate String @Id"); 
    }
}

Solution

  • Please have a look at the JUnit wiki: https://github.com/junit-team/junit/wiki/Exception-testing It lists different approaches for testing exceptions.