Merge pull request #12105 from Alex-Golub/study/assert-exception

JUnit5 conventions + code rearrangement
This commit is contained in:
Loredana Crusoveanu 2022-05-27 21:18:24 +03:00 committed by GitHub
commit f1dfbfe5b8
1 changed files with 9 additions and 11 deletions

View File

@ -1,16 +1,15 @@
package com.baeldung.assertexception; package com.baeldung.assertexception;
import org.junit.jupiter.api.Test;
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 static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test; class ExceptionAssertionUnitTest {
public class ExceptionAssertionUnitTest {
@Test @Test
public void whenExceptionThrown_thenAssertionSucceeds() { void whenExceptionThrown_thenAssertionSucceeds() {
Exception exception = assertThrows(NumberFormatException.class, () -> { Exception exception = assertThrows(NumberFormatException.class, () -> Integer.parseInt("1a"));
Integer.parseInt("1a");
});
String expectedMessage = "For input string"; String expectedMessage = "For input string";
String actualMessage = exception.getMessage(); String actualMessage = exception.getMessage();
@ -19,14 +18,13 @@ public class ExceptionAssertionUnitTest {
} }
@Test @Test
public void whenDerivedExceptionThrown_thenAssertionSucceds() { void whenDerivedExceptionThrown_thenAssertionSucceeds() {
Exception exception = assertThrows(RuntimeException.class, () -> { Exception exception = assertThrows(RuntimeException.class, () -> Integer.parseInt("1a"));
Integer.parseInt("1a");
});
String expectedMessage = "For input string"; String expectedMessage = "For input string";
String actualMessage = exception.getMessage(); String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage)); assertTrue(actualMessage.contains(expectedMessage));
} }
} }