Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

how to assert that an exception is thrown java

@Test
public void whenExceptionThrown_thenAssertionSucceeds() {
    Exception exception = assertThrows(NumberFormatException.class, () -> {
        Integer.parseInt("1a");
    });

    String expectedMessage = "For input string";
    String actualMessage = exception.getMessage();

    assertTrue(actualMessage.contains(expectedMessage));
}
Source by www.baeldung.com #
 
PREVIOUS NEXT
Tagged: #assert #exception #thrown #java
ADD COMMENT
Topic
Name
8+9 =