assertHandlesException

fun <T : Any> assertHandlesException(throwable: Throwable, mockSetup: () -> Unit, verificationBlock: () -> T)

A test utility to verify that a block of code correctly handles an exception.

This function will:

  1. Set up a mock to throw a given throwable.

  2. Execute the verificationBlock which is expected to contain a try-catch block.

  3. Assert that the verificationBlock does not throw an unhandled exception.

Parameters

T

The return type of the function being tested.

throwable

The exception to be thrown by the mock.

mockSetup

A lambda that defines the mock behavior that will throw the throwable.

verificationBlock

The main block of code to be executed, which should handle the exception.