https://www.baeldung.com/junit-5-extensions
JUnit 5 does not have rules but has extensions.
In the Junit 4 rules example we had the following test:
@Test
public void testDiv()
{
System.out.println( "Running Exception test: testDiv" ) ;
int x1 ;
thrown.expect( ArithmeticException.class );
x1 = 5 / 0 ;
}
Include this test in the JUnit5. Write your own Extension of the form:
import org.junit.jupiter.api.extension.* ;
import static org.junit.jupiter.api.Assertions.* ;
public class MyExt2 implements TestExecutionExceptionHandler
{
@Override
public void handleTestExecutionException(ExtensionContext context,
Throwable throwable) throws Throwable
{
//TO DO
}
Modify "testDiv" to use this extension to provide the same functionality of the JUnit4 test and that is if there is an Exception of the form "ArithmeticException" then the test is a success. Add also the condition that if another kind of Exception is thrown then that is considered a failure.