This commit is contained in:
parent
b7253c639b
commit
ff77450cee
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue