Class ExpectedExceptionSupport

java.lang.Object
org.junit.jupiter.migrationsupport.rules.ExpectedExceptionSupport
All Implemented Interfaces:
AfterEachCallback, Extension, TestExecutionExceptionHandler

@API(status=STABLE, since="5.7") public class ExpectedExceptionSupport extends Object implements AfterEachCallback, TestExecutionExceptionHandler
This Extension provides native support for the ExpectedException rule from JUnit 4.

By using this class-level extension on a test class, ExpectedException can continue to be used.

However, you should rather switch to Assertions.assertThrows(java.lang.Class<T>, org.junit.jupiter.api.function.Executable) for new code.

Since:
5.0
See Also:
  • Constructor Details

    • ExpectedExceptionSupport

      public ExpectedExceptionSupport()
  • Method Details

    • handleTestExecutionException

      public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable
      Description copied from interface: TestExecutionExceptionHandler
      Handle the supplied throwable.

      Implementors must perform one of the following.

      1. Swallow the supplied throwable, thereby preventing propagation.
      2. Rethrow the supplied throwable as is.
      3. Throw a new exception, potentially wrapping the supplied throwable.

      If the supplied throwable is swallowed, subsequent TestExecutionExceptionHandlers will not be invoked; otherwise, the next registered TestExecutionExceptionHandler (if there is one) will be invoked with any Throwable thrown by this handler.

      Note that the execution exception in the supplied ExtensionContext will not contain the Throwable thrown during invocation of the corresponding @Test method.

      Specified by:
      handleTestExecutionException in interface TestExecutionExceptionHandler
      Parameters:
      context - the current extension context; never null
      throwable - the Throwable to handle; never null
      Throws:
      Throwable
    • afterEach

      public void afterEach(ExtensionContext context) throws Exception
      Description copied from interface: AfterEachCallback
      Callback that is invoked after an individual test and any user-defined teardown methods for that test have been executed.
      Specified by:
      afterEach in interface AfterEachCallback
      Parameters:
      context - the current extension context; never null
      Throws:
      Exception