This commit is contained in:
parent
b7253c639b
commit
ff77450cee
|
@ -1,23 +1,32 @@
|
||||||
package com.baeldung.assertexception;
|
package com.baeldung.assertexception;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class ExceptionAssertionUnitTest {
|
public class ExceptionAssertionUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenExceptionThrown_thenAssertionSucceeds() {
|
public void whenExceptionThrown_thenAssertionSucceeds() {
|
||||||
String test = null;
|
Exception exception = assertThrows(NumberFormatException.class, () -> {
|
||||||
assertThrows(NullPointerException.class, () -> {
|
Integer.parseInt("1a");
|
||||||
test.length();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
String expectedMessage = "For input string";
|
||||||
|
String actualMessage = exception.getMessage();
|
||||||
|
|
||||||
|
assertTrue(actualMessage.contains(expectedMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDerivedExceptionThrown_thenAssertionSucceds() {
|
public void whenDerivedExceptionThrown_thenAssertionSucceds() {
|
||||||
String test = null;
|
Exception exception = assertThrows(RuntimeException.class, () -> {
|
||||||
assertThrows(RuntimeException.class, () -> {
|
Integer.parseInt("1a");
|
||||||
test.length();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
String expectedMessage = "For input string";
|
||||||
|
String actualMessage = exception.getMessage();
|
||||||
|
|
||||||
|
assertTrue(actualMessage.contains(expectedMessage));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue