mikr 2019-12-08 19:22:06 +01:00
parent b7253c639b
commit ff77450cee
1 changed files with 15 additions and 6 deletions

View File

@ -1,23 +1,32 @@
package com.baeldung.assertexception;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class ExceptionAssertionUnitTest {
@Test
public void whenExceptionThrown_thenAssertionSucceeds() {
String test = null;
assertThrows(NullPointerException.class, () -> {
test.length();
Exception exception = assertThrows(NumberFormatException.class, () -> {
Integer.parseInt("1a");
});
String expectedMessage = "For input string";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
@Test
public void whenDerivedExceptionThrown_thenAssertionSucceds() {
String test = null;
assertThrows(RuntimeException.class, () -> {
test.length();
Exception exception = assertThrows(RuntimeException.class, () -> {
Integer.parseInt("1a");
});
String expectedMessage = "For input string";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
}