Merge pull request #8332 from Maiklins/BAEL-3586

BAEL-3586
This commit is contained in:
Eric Martin 2019-12-13 21:35:30 -06:00 committed by GitHub
commit 0e70140e25

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));
}
}