Skip to content

Commit 111afc3

Browse files
ExpectedFailure.when: Extend using an optional failure mapper
1 parent e83e347 commit 111afc3

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/main/java/org/cornutum/hamcrest/ExpectedFailure.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.util.Optional;
1111
import java.util.function.Consumer;
12+
import java.util.function.Function;
1213
import java.util.function.Supplier;
1314

1415
/**
@@ -30,12 +31,22 @@ public ExpectedFailure( Class<T> failureType)
3031
/**
3132
* Throws an AssertionError if the expected Throwable is not thrown by the given action.
3233
*/
33-
@SuppressWarnings("unchecked")
3434
public ExpectedFailure<T> when( Failable action)
35+
{
36+
return when( action, Function.identity());
37+
}
38+
39+
/**
40+
* Throws an AssertionError if the expected Throwable is not thrown by the given action.
41+
* The expected Throwable is defined by applying the <CODE>failureMapper</CODE> to the
42+
* Throwable produced by the action.
43+
*/
44+
@SuppressWarnings("unchecked")
45+
public ExpectedFailure<T> when( Failable action, Function<Throwable,Throwable> failureMapper)
3546
{
3647
expected = Optional.empty();
3748

38-
Throwable failure = action.get().orElse( null);
49+
Throwable failure = action.get().map( t -> failureMapper.apply(t)).orElse( null);
3950
if( failure == null)
4051
{
4152
throw new AssertionError( "Expected " + failureType.getSimpleName() + " was not thrown");

0 commit comments

Comments
 (0)