Class ThrowableAsserter<R>
- java.lang.Object
-
- com.github.robtimus.junit.support.ThrowableAsserter<R>
-
- Type Parameters:
R- The result type of the code to execute.
public final class ThrowableAsserter<R> extends Object
An object that asserts that executing anExecutableor retrieving the value of aThrowingSupplierthrows an error.This class is like several of the throwing assertions of
ThrowableAssertions, especially the "one-of" assertions, but it provides more flexibility. It also removes the need for checking the return type of the returned error in case of the "one-of" assertions; instead, a set of assertions can be configured per expected error type (or none to just specify that the error type is one of the expected error types).This class should be used as follows:
- Call one of the static
whenThrowsorwhenThrowsExactlymethods to create an instance, and specify the assertions for that error type usingthenAssertorthenAssertNothing. - Call
whenThrowsandwhenThrowsExactlyany number of times and in any order, and specify the assertions for that error type usingthenAssertorthenAssertNothing.
However, all calls must be unique, i.e. you cannot callwhenThrowstwice with the same type, or callwhenThrowsExactlytwice with the same type - Call
whenThrowsNothingat most once, and specify the assertions for no error usingthenAssertorthenAssertNothing. - Call
execute. This will execute theExecutableor retrieve the value of theThrowingSupplier, and perform the necessary assertions. - Optionally, use the return value of the
executemethod to retrieve the error that was thrown or theThrowingSupplier's value.
An example:
whenThrows(UnsupportedOperationException.class, () -> map.computeIfAbsent(key, function)).thenAssertNothing() .whenThrows(IllegalArgumentException.class).thenAssert(thrown -> assertSame(exception, thrown)) .execute();All methods throw a
NullPointerExceptionwhen provided withnullarguments unless specified otherwise.- Author:
- Rob Spoor
- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classThrowableAsserter.Asserted<R>An object that represents aThrowableAsserterin its asserted state.static classThrowableAsserter.NoError<R>An object that can be used to configure the assertions that should be performed when no error is thrown.static classThrowableAsserter.ThrownError<T extends Throwable,R>An object that can be used to configure the assertions that should be performed when an error is thrown.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ThrowableAsserter.Asserted<R>execute()Executes theExecutableor retrieves the value of theThrowingSupplierused to create this object, and perform the necessary assertions.ThrowableAsserter.Asserted<R>execute(String message)Executes theExecutableor retrieves the value of theThrowingSupplierused to create this object, and perform the necessary assertions.ThrowableAsserter.Asserted<R>execute(Supplier<String> messageSupplier)Executes theExecutableor retrieves the value of theThrowingSupplierused to create this object, and perform the necessary assertions.<T extends Throwable>
ThrowableAsserter.ThrownError<T,R>whenThrows(Class<T> errorType)Returns an object for configuring the assertions that should be performed when an instance of a specific error type is thrown.static <T extends Throwable>
ThrowableAsserter.ThrownError<T,Void>whenThrows(Class<T> errorType, Executable executable)Returns an object for configuring the assertions that should be performed when an instance of a specific error type is thrown when anExecutableis run.static <T extends Throwable,R>
ThrowableAsserter.ThrownError<T,R>whenThrows(Class<T> errorType, ThrowingSupplier<R> supplier)Returns an object for configuring the assertions that should be performed when an instance of a specific error type is thrown when the result of aThrowingSupplieris retrieved.<T extends Throwable>
ThrowableAsserter.ThrownError<T,R>whenThrowsExactly(Class<T> errorType)Returns an object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown.static <T extends Throwable>
ThrowableAsserter.ThrownError<T,Void>whenThrowsExactly(Class<T> errorType, Executable executable)Returns an object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown when anExecutableis run.static <T extends Throwable,R>
ThrowableAsserter.ThrownError<T,R>whenThrowsExactly(Class<T> errorType, ThrowingSupplier<R> supplier)Returns an object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown when the result of aThrowingSupplieris retrieved.ThrowableAsserter.NoError<R>whenThrowsNothing()Returns an object for configuring the assertions that should be performed if no error is thrown.
-
-
-
Method Detail
-
whenThrows
public static <T extends Throwable> ThrowableAsserter.ThrownError<T,Void> whenThrows(Class<T> errorType, Executable executable)
Returns an object for configuring the assertions that should be performed when an instance of a specific error type is thrown when anExecutableis run.- Type Parameters:
T- The error type.- Parameters:
errorType- The error type.executable- TheExecutableto run.- Returns:
- An object for configuring the assertions that should be performed when an instance of a specific error type is thrown.
- See Also:
Assertions.assertThrows(Class, Executable)
-
whenThrows
public static <T extends Throwable,R> ThrowableAsserter.ThrownError<T,R> whenThrows(Class<T> errorType, ThrowingSupplier<R> supplier)
Returns an object for configuring the assertions that should be performed when an instance of a specific error type is thrown when the result of aThrowingSupplieris retrieved.- Type Parameters:
T- The error type.R- The result type of theThrowingSupplier.- Parameters:
errorType- The error type.supplier- TheThrowingSupplierwith the result to retrieve.- Returns:
- An object for configuring the assertions that should be performed when an instance of a specific error type is thrown.
- See Also:
Assertions.assertThrows(Class, Executable)
-
whenThrows
public <T extends Throwable> ThrowableAsserter.ThrownError<T,R> whenThrows(Class<T> errorType)
Returns an object for configuring the assertions that should be performed when an instance of a specific error type is thrown.- Type Parameters:
T- The error type.- Parameters:
errorType- The error type.- Returns:
- An object for configuring the assertions that should be performed when an instance of a specific error type is thrown.
- Throws:
IllegalArgumentException- If this method has already been called with the given error type.- See Also:
Assertions.assertThrows(Class, Executable)
-
whenThrowsExactly
public static <T extends Throwable> ThrowableAsserter.ThrownError<T,Void> whenThrowsExactly(Class<T> errorType, Executable executable)
Returns an object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown when anExecutableis run.- Type Parameters:
T- The error type.- Parameters:
errorType- The error type.executable- TheExecutableto run.- Returns:
- An object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown.
- See Also:
Assertions.assertThrowsExactly(Class, Executable)
-
whenThrowsExactly
public static <T extends Throwable,R> ThrowableAsserter.ThrownError<T,R> whenThrowsExactly(Class<T> errorType, ThrowingSupplier<R> supplier)
Returns an object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown when the result of aThrowingSupplieris retrieved.- Type Parameters:
T- The error type.R- The result type of theThrowingSupplier.- Parameters:
errorType- The error type.supplier- TheThrowingSupplierwith the result to retrieve.- Returns:
- An object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown.
- See Also:
Assertions.assertThrowsExactly(Class, Executable)
-
whenThrowsExactly
public <T extends Throwable> ThrowableAsserter.ThrownError<T,R> whenThrowsExactly(Class<T> errorType)
Returns an object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown.- Type Parameters:
T- The error type.- Parameters:
errorType- The error type.- Returns:
- An object for configuring the assertions that should be performed when an exact instance of a specific error type is thrown.
- Throws:
IllegalArgumentException- If this method has already been called with the given error type.- See Also:
Assertions.assertThrowsExactly(Class, Executable)
-
whenThrowsNothing
public ThrowableAsserter.NoError<R> whenThrowsNothing()
Returns an object for configuring the assertions that should be performed if no error is thrown.If this method is not called,
execute(),execute(String)andexecute(Supplier)will fail if no error is thrown.- Returns:
- An object for configuring the assertions that should be performed if no error is thrown.
- Throws:
IllegalStateException- If this method is called without configuring the assertions for at least one error type, or If this method has already been called.
-
execute
public ThrowableAsserter.Asserted<R> execute()
Executes theExecutableor retrieves the value of theThrowingSupplierused to create this object, and perform the necessary assertions.- Returns:
- An object that represents this object in its asserted state.
- Throws:
AssertionFailedError- If an error is thrown that is not an instance of one of the configured error types, or if no error is thrown andwhenThrowsNothing()has not been called.
-
execute
public ThrowableAsserter.Asserted<R> execute(String message)
Executes theExecutableor retrieves the value of theThrowingSupplierused to create this object, and perform the necessary assertions.- Parameters:
message- The failure message to fail with; may benull.- Returns:
- An object that represents this object in its asserted state.
- Throws:
AssertionFailedError- If an error is thrown that is not an instance of one of the configured error types, or if no error is thrown andwhenThrowsNothing()has not been called.
-
execute
public ThrowableAsserter.Asserted<R> execute(Supplier<String> messageSupplier)
Executes theExecutableor retrieves the value of theThrowingSupplierused to create this object, and perform the necessary assertions.- Parameters:
messageSupplier- The supplier for the failure message to fail with; may benull.- Returns:
- An object that represents this object in its asserted state.
- Throws:
AssertionFailedError- If an error is thrown that is not an instance of one of the configured error types, or if no error is thrown andwhenThrowsNothing()has not been called.
-
-