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 anExecutable
or retrieving the value of aThrowingSupplier
throws 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
whenThrows
orwhenThrowsExactly
methods to create an instance, and specify the assertions for that error type usingthenAssert
orthenAssertNothing
. - Call
whenThrows
andwhenThrowsExactly
any number of times and in any order, and specify the assertions for that error type usingthenAssert
orthenAssertNothing
.
However, all calls must be unique, i.e. you cannot callwhenThrows
twice with the same type, or callwhenThrowsExactly
twice with the same type - Call
whenThrowsNothing
at most once, and specify the assertions for no error usingthenAssert
orthenAssertNothing
. - Call
execute
. This will execute theExecutable
or retrieve the value of theThrowingSupplier
, and perform the necessary assertions. - Optionally, use the return value of the
execute
method 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
NullPointerException
when provided withnull
arguments unless specified otherwise.- Author:
- Rob Spoor
- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ThrowableAsserter.Asserted<R>
An object that represents aThrowableAsserter
in its asserted state.static class
ThrowableAsserter.NoError<R>
An object that can be used to configure the assertions that should be performed when no error is thrown.static class
ThrowableAsserter.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 theExecutable
or retrieves the value of theThrowingSupplier
used to create this object, and perform the necessary assertions.ThrowableAsserter.Asserted<R>
execute(String message)
Executes theExecutable
or retrieves the value of theThrowingSupplier
used to create this object, and perform the necessary assertions.ThrowableAsserter.Asserted<R>
execute(Supplier<String> messageSupplier)
Executes theExecutable
or retrieves the value of theThrowingSupplier
used 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 anExecutable
is 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 aThrowingSupplier
is 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 anExecutable
is 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 aThrowingSupplier
is 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 anExecutable
is run.- Type Parameters:
T
- The error type.- Parameters:
errorType
- The error type.executable
- TheExecutable
to 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 aThrowingSupplier
is retrieved.- Type Parameters:
T
- The error type.R
- The result type of theThrowingSupplier
.- Parameters:
errorType
- The error type.supplier
- TheThrowingSupplier
with 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 anExecutable
is run.- Type Parameters:
T
- The error type.- Parameters:
errorType
- The error type.executable
- TheExecutable
to 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 aThrowingSupplier
is retrieved.- Type Parameters:
T
- The error type.R
- The result type of theThrowingSupplier
.- Parameters:
errorType
- The error type.supplier
- TheThrowingSupplier
with 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 theExecutable
or retrieves the value of theThrowingSupplier
used 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 theExecutable
or retrieves the value of theThrowingSupplier
used 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 theExecutable
or retrieves the value of theThrowingSupplier
used 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.
-
-