Skip to content

Commit c3e10c0

Browse files
add missing thenCode assertion
1 parent 4d1e1e1 commit c3e10c0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/main/java/org/assertj/core/api/Java6BDDAssertions.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,46 @@ public static AbstractDateAssert<?> then(Date actual) {
839839
return assertThatThrownBy(shouldRaiseThrowable);
840840
}
841841

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 = () -&gt; {
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 = () -&gt; {
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+
842882
/**
843883
* Creates a new instance of <code>{@link UriAssert}</code>.
844884
*

0 commit comments

Comments
 (0)