Skip to content

Commit

Permalink
Revert "Provide value when assertThatThrownBy/thenThrownBy fail" (#3318)
Browse files Browse the repository at this point in the history
This reverts commit cf06398
and adds a new test to prevent further regression.
  • Loading branch information
scordio committed Jan 2, 2024
1 parent 887f97b commit 2a7c5a6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 125 deletions.
43 changes: 0 additions & 43 deletions assertj-core/src/main/java/org/assertj/core/api/Assertions.java
Expand Up @@ -12,7 +12,6 @@
*/
package org.assertj.core.api;

import static java.lang.String.format;
import static org.assertj.core.configuration.ConfigurationProvider.CONFIGURATION_PROVIDER;
import static org.assertj.core.data.Percentage.withPercentage;

Expand Down Expand Up @@ -76,7 +75,6 @@
import java.util.stream.Stream;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.api.ThrowableAssert.ThrowingCallableWithValue;
import org.assertj.core.api.filter.FilterOperator;
import org.assertj.core.api.filter.Filters;
import org.assertj.core.api.filter.InFilter;
Expand All @@ -98,7 +96,6 @@
import org.assertj.core.description.Description;
import org.assertj.core.groups.Properties;
import org.assertj.core.groups.Tuple;
import org.assertj.core.internal.Failures;
import org.assertj.core.presentation.BinaryRepresentation;
import org.assertj.core.presentation.HexadecimalRepresentation;
import org.assertj.core.presentation.Representation;
Expand Down Expand Up @@ -1212,26 +1209,6 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> assertThat(T a
return assertThat(catchThrowable(shouldRaiseThrowable)).hasBeenThrown();
}

/**
* Similar to {@link #assertThatThrownBy(ThrowingCallable)}, but when the called code returns a value instead of
* throwing, the assertion error shows the returned value to help understand what went wrong.
*
* @param shouldRaiseThrowable The {@link ThrowingCallableWithValue} or lambda with the code that should raise the throwable.
* @return the created {@link ThrowableAssert}.
* @since 3.25.0
*/
@CanIgnoreReturnValue
public static AbstractThrowableAssert<?, ? extends Throwable> assertThatThrownBy(
ThrowingCallableWithValue shouldRaiseThrowable) {
Object value;
try {
value = shouldRaiseThrowable.call();
} catch (Throwable throwable) {
return assertThat(throwable);
}
throw Failures.instance().failure(format("Expecting code to raise a throwable, but it returned [%s] instead", value));
}

/**
* Allows to capture and then assert on a {@link Throwable} like {@code assertThatThrownBy(ThrowingCallable)} but this method
* let you set the assertion description the same way you do with {@link AbstractAssert#as(String, Object...) as(String, Object...)}.
Expand Down Expand Up @@ -1270,26 +1247,6 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> assertThat(T a
return assertThat(catchThrowable(shouldRaiseThrowable)).as(description, args).hasBeenThrown();
}

/**
* Similar to {@link #assertThatThrownBy(ThrowingCallable, String, Object...)}, but when the called code returns a value
* instead of throwing, the assertion error shows the returned value to help understand what went wrong.
*
* @param shouldRaiseThrowable The {@link ThrowingCallableWithValue} or lambda with the code that should raise the throwable.
* @return the created {@link ThrowableAssert}.
* @since 3.25.0
*/
@CanIgnoreReturnValue
public static AbstractThrowableAssert<?, ? extends Throwable> assertThatThrownBy(ThrowingCallableWithValue shouldRaiseThrowable,
String description, Object... args) {
Object value;
try {
value = shouldRaiseThrowable.call();
} catch (Throwable throwable) {
return assertThat(throwable).as(description, args);
}
throw Failures.instance().failure(format("Expecting code to raise a throwable, but it returned [%s] instead", value));
}

/**
* Allows to capture and then assert on a {@link Throwable} (easier done with lambdas).
* <p>
Expand Down
43 changes: 0 additions & 43 deletions assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java
Expand Up @@ -12,8 +12,6 @@
*/
package org.assertj.core.api;

import static java.lang.String.format;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -74,7 +72,6 @@
import java.util.stream.Stream;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.api.ThrowableAssert.ThrowingCallableWithValue;
import org.assertj.core.api.filter.FilterOperator;
import org.assertj.core.api.filter.InFilter;
import org.assertj.core.api.filter.NotFilter;
Expand All @@ -92,7 +89,6 @@
import org.assertj.core.description.Description;
import org.assertj.core.groups.Properties;
import org.assertj.core.groups.Tuple;
import org.assertj.core.internal.Failures;
import org.assertj.core.presentation.BinaryRepresentation;
import org.assertj.core.presentation.HexadecimalRepresentation;
import org.assertj.core.presentation.Representation;
Expand Down Expand Up @@ -1334,25 +1330,6 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> then(T actual)
return assertThat(catchThrowable(shouldRaiseThrowable)).hasBeenThrown();
}

/**
* Similar to {@link #thenThrownBy(ThrowingCallable)}, but when the called code returns a value instead of
* throwing, the assertion error shows the returned value to help understand what went wrong.
*
* @param shouldRaiseThrowable The {@link ThrowingCallableWithValue} or lambda with the code that should raise the throwable.
* @return the created {@link ThrowableAssert}.
* @since 3.25.0
*/
@CanIgnoreReturnValue
public static AbstractThrowableAssert<?, ? extends Throwable> thenThrownBy(ThrowingCallableWithValue shouldRaiseThrowable) {
Object value;
try {
value = shouldRaiseThrowable.call();
} catch (Throwable throwable) {
return assertThat(throwable);
}
throw Failures.instance().failure(format("Expecting code to raise a throwable, but it returned [%s] instead", value));
}

/**
* Allows to capture and then assert on a {@link Throwable} like {@code thenThrownBy(ThrowingCallable)} but this method
* let you set the assertion description the same way you do with {@link AbstractAssert#as(String, Object...) as(String, Object...)}.
Expand Down Expand Up @@ -1390,26 +1367,6 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> then(T actual)
return assertThat(catchThrowable(shouldRaiseThrowable)).as(description, args).hasBeenThrown();
}

/**
* Similar to {@link #thenThrownBy(ThrowingCallable, String, Object...)}, but when the called code returns a value instead of
* throwing, the assertion error shows the returned value to help understand what went wrong.
*
* @param shouldRaiseThrowable The {@link ThrowingCallableWithValue} or lambda with the code that should raise the throwable.
* @return the created {@link ThrowableAssert}.
* @since 3.25.0
*/
@CanIgnoreReturnValue
public static AbstractThrowableAssert<?, ? extends Throwable> thenThrownBy(ThrowingCallableWithValue shouldRaiseThrowable,
String description, Object... args) {
Object value;
try {
value = shouldRaiseThrowable.call();
} catch (Throwable throwable) {
return assertThat(throwable).as(description, args);
}
throw Failures.instance().failure(format("Expecting code to raise a throwable, but it returned [%s] instead", value));
}

/**
* Allows to capture and then assert on a {@link Throwable} more easily when used with Java 8 lambdas.
*
Expand Down
Expand Up @@ -33,10 +33,6 @@ public interface ThrowingCallable {
void call() throws Throwable;
}

public interface ThrowingCallableWithValue {
Object call() throws Throwable;
}

public ThrowableAssert(ACTUAL actual) {
super(actual, ThrowableAssert.class);
}
Expand Down
Expand Up @@ -14,9 +14,8 @@

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions_catchThrowableOfType_Test.raisingException;
import static org.assertj.core.api.Assertions_catchThrowable_Test.codeThrowing;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldHaveMessage.shouldHaveMessage;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
Expand All @@ -25,47 +24,63 @@
class Assertions_assertThatThrownBy_Test {

@Test
void should_build_ThrowableAssert_with_runtime_exception_thrown() {
void should_work_with_runtime_exception_thrown() {
// WHEN/THEN
assertThatThrownBy(codeThrowing(new IllegalArgumentException("boom"))).isInstanceOf(IllegalArgumentException.class)
.hasMessage("boom");
}

@Test
void should_build_ThrowableAssert_with_throwable_thrown() {
void should_work_with_throwable_thrown() {
// WHEN/THEN
assertThatThrownBy(codeThrowing(new Throwable("boom"))).isInstanceOf(Throwable.class)
.hasMessage("boom");
}

@Test
void should_be_able_to_pass_a_description_to_assertThatThrownBy() {
void should_work_with_method_reference_having_vararg_parameter() {
// WHEN/THEN
assertThatThrownBy(Assertions_assertThatThrownBy_Test::methodThrowing).isInstanceOf(Exception.class)
.hasMessage("boom");
}

@Test
void should_support_description() {
// GIVEN
// make assertThatThrownBy fail to verify the description afterwards
ThrowingCallable code = () -> assertThatThrownBy(raisingException("boom"), "Test %s", "code").hasMessage("bam");
Throwable throwable = new Exception("boom");
// WHEN
AssertionError assertionError = expectAssertionError(code);
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(codeThrowing(throwable), "Test %s",
"code").hasMessage("bam"));
// THEN
then(assertionError).hasMessageContaining("[Test code]");
}

@Test
void should_fail_if_no_throwable_was_thrown() {
// GIVEN
ThrowingCallable code = () -> {};
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(code).hasMessage("boom ?"));
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(() -> {}).hasMessage("boom ?"));
// THEN
then(assertionError).hasMessage(format("%nExpecting code to raise a throwable."));
}

@Test
void should_fail_with_good_message_when_assertion_is_failing() {
void should_fail_with_proper_message_when_assertion_is_failing() {
// GIVEN
Throwable throwable = new Exception("boom");
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(raisingException("boom")).hasMessage("bam"));
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(codeThrowing(throwable)).hasMessage("bam"));
// THEN
then(assertionError).hasMessageContainingAll("Expecting message to be:",
"\"bam\"",
"but was:",
"\"boom\"");
then(assertionError).hasMessage(shouldHaveMessage(throwable, "bam").create());
}

private static ThrowingCallable codeThrowing(Throwable t) {
return () -> {
throw t;
};
}

private static void methodThrowing(Object... parameters) throws Exception {
throw new Exception("boom");
}

}
Expand Up @@ -12,10 +12,8 @@
*/
package org.assertj.core.api.throwable;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -52,14 +50,7 @@ public void call() {
// no exception
}
});
}).withMessage(format("%nExpecting code to raise a throwable."));
}).withMessage(String.format("%nExpecting code to raise a throwable."));
}

@Test
void should_fail_and_show_value_returned_by_callable_code() {
// GIVEN
ThrowingCallable code = () -> assertThatThrownBy(() -> 42);
// WHEN/THEN
assertThatAssertionErrorIsThrownBy(code).withMessage("Expecting code to raise a throwable, but it returned [42] instead");
}
}
Expand Up @@ -14,11 +14,11 @@

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.Test;

// TODO build two throwable assert with then and assertThat and compare them.
class ThrowableAssert_built_with_then_method_Test {

@Test
Expand Down Expand Up @@ -54,11 +54,4 @@ public void call() {
}).withMessage(String.format("%nExpecting code to raise a throwable."));
}

@Test
void should_fail_if_value_is_returned_by_callable_code() {
// GIVEN
ThrowingCallable code = () -> thenThrownBy(() -> 42);
// WHEN/THEN
assertThatAssertionErrorIsThrownBy(code).withMessage("Expecting code to raise a throwable, but it returned [42] instead");
}
}

0 comments on commit 2a7c5a6

Please sign in to comment.