@@ -839,6 +839,46 @@ public static AbstractDateAssert<?> then(Date actual) {
839
839
return assertThatThrownBy (shouldRaiseThrowable );
840
840
}
841
841
842
+ /**
843
+ * Allows to capture and then assert on a {@link Throwable} more easily when used with Java 8 lambdas.
844
+ *
845
+ * <p>
846
+ * Example :
847
+ * </p>
848
+ *
849
+ * <pre><code class='java'> ThrowingCallable callable = () -> {
850
+ * throw new Exception("boom!");
851
+ * };
852
+ *
853
+ * // assertion succeeds
854
+ * thenCode(callable).isInstanceOf(Exception.class)
855
+ * .hasMessageContaining("boom");
856
+ *
857
+ * // assertion fails
858
+ * thenCode(callable).doesNotThrowAnyException();</code></pre>
859
+ *
860
+ * Contrary to {@link #thenThrownBy(ThrowingCallable)} the test description provided with
861
+ * {@link AbstractAssert#as(String, Object...) as(String, Object...)} is always honored as shown below.
862
+ *
863
+ * <pre><code class='java'> ThrowingCallable doNothing = () -> {
864
+ * // do nothing
865
+ * };
866
+ *
867
+ * // assertion fails and "display me" appears in the assertion error
868
+ * thenCode(doNothing).as("display me")
869
+ * .isInstanceOf(Exception.class);</code></pre>
870
+ * <p>
871
+ * This method was not named {@code then} because the java compiler reported it ambiguous when used directly with a lambda :(
872
+ *
873
+ * @param shouldRaiseOrNotThrowable The {@link ThrowingCallable} or lambda with the code that should raise the throwable.
874
+ * @return The captured exception or <code>null</code> if none was raised by the callable.
875
+ * @since 3.7.0
876
+ */
877
+ @ CheckReturnValue
878
+ public AbstractThrowableAssert <?, ? extends Throwable > thenCode (ThrowingCallable shouldRaiseOrNotThrowable ) {
879
+ return then (catchThrowable (shouldRaiseOrNotThrowable ));
880
+ }
881
+
842
882
/**
843
883
* Creates a new instance of <code>{@link UriAssert}</code>.
844
884
*
0 commit comments