Skip to content

Commit 2a7c5a6

Browse files
authored
Revert "Provide value when assertThatThrownBy/thenThrownBy fail" (#3318)
This reverts commit cf06398 and adds a new test to prevent further regression.
1 parent 887f97b commit 2a7c5a6

6 files changed

Lines changed: 34 additions & 125 deletions

File tree

assertj-core/src/main/java/org/assertj/core/api/Assertions.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
package org.assertj.core.api;
1414

15-
import static java.lang.String.format;
1615
import static org.assertj.core.configuration.ConfigurationProvider.CONFIGURATION_PROVIDER;
1716
import static org.assertj.core.data.Percentage.withPercentage;
1817

@@ -76,7 +75,6 @@
7675
import java.util.stream.Stream;
7776

7877
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
79-
import org.assertj.core.api.ThrowableAssert.ThrowingCallableWithValue;
8078
import org.assertj.core.api.filter.FilterOperator;
8179
import org.assertj.core.api.filter.Filters;
8280
import org.assertj.core.api.filter.InFilter;
@@ -98,7 +96,6 @@
9896
import org.assertj.core.description.Description;
9997
import org.assertj.core.groups.Properties;
10098
import org.assertj.core.groups.Tuple;
101-
import org.assertj.core.internal.Failures;
10299
import org.assertj.core.presentation.BinaryRepresentation;
103100
import org.assertj.core.presentation.HexadecimalRepresentation;
104101
import org.assertj.core.presentation.Representation;
@@ -1212,26 +1209,6 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> assertThat(T a
12121209
return assertThat(catchThrowable(shouldRaiseThrowable)).hasBeenThrown();
12131210
}
12141211

1215-
/**
1216-
* Similar to {@link #assertThatThrownBy(ThrowingCallable)}, but when the called code returns a value instead of
1217-
* throwing, the assertion error shows the returned value to help understand what went wrong.
1218-
*
1219-
* @param shouldRaiseThrowable The {@link ThrowingCallableWithValue} or lambda with the code that should raise the throwable.
1220-
* @return the created {@link ThrowableAssert}.
1221-
* @since 3.25.0
1222-
*/
1223-
@CanIgnoreReturnValue
1224-
public static AbstractThrowableAssert<?, ? extends Throwable> assertThatThrownBy(
1225-
ThrowingCallableWithValue shouldRaiseThrowable) {
1226-
Object value;
1227-
try {
1228-
value = shouldRaiseThrowable.call();
1229-
} catch (Throwable throwable) {
1230-
return assertThat(throwable);
1231-
}
1232-
throw Failures.instance().failure(format("Expecting code to raise a throwable, but it returned [%s] instead", value));
1233-
}
1234-
12351212
/**
12361213
* Allows to capture and then assert on a {@link Throwable} like {@code assertThatThrownBy(ThrowingCallable)} but this method
12371214
* let you set the assertion description the same way you do with {@link AbstractAssert#as(String, Object...) as(String, Object...)}.
@@ -1270,26 +1247,6 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> assertThat(T a
12701247
return assertThat(catchThrowable(shouldRaiseThrowable)).as(description, args).hasBeenThrown();
12711248
}
12721249

1273-
/**
1274-
* Similar to {@link #assertThatThrownBy(ThrowingCallable, String, Object...)}, but when the called code returns a value
1275-
* instead of throwing, the assertion error shows the returned value to help understand what went wrong.
1276-
*
1277-
* @param shouldRaiseThrowable The {@link ThrowingCallableWithValue} or lambda with the code that should raise the throwable.
1278-
* @return the created {@link ThrowableAssert}.
1279-
* @since 3.25.0
1280-
*/
1281-
@CanIgnoreReturnValue
1282-
public static AbstractThrowableAssert<?, ? extends Throwable> assertThatThrownBy(ThrowingCallableWithValue shouldRaiseThrowable,
1283-
String description, Object... args) {
1284-
Object value;
1285-
try {
1286-
value = shouldRaiseThrowable.call();
1287-
} catch (Throwable throwable) {
1288-
return assertThat(throwable).as(description, args);
1289-
}
1290-
throw Failures.instance().failure(format("Expecting code to raise a throwable, but it returned [%s] instead", value));
1291-
}
1292-
12931250
/**
12941251
* Allows to capture and then assert on a {@link Throwable} (easier done with lambdas).
12951252
* <p>

assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
*/
1313
package org.assertj.core.api;
1414

15-
import static java.lang.String.format;
16-
1715
import java.io.File;
1816
import java.io.IOException;
1917
import java.io.InputStream;
@@ -74,7 +72,6 @@
7472
import java.util.stream.Stream;
7573

7674
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
77-
import org.assertj.core.api.ThrowableAssert.ThrowingCallableWithValue;
7875
import org.assertj.core.api.filter.FilterOperator;
7976
import org.assertj.core.api.filter.InFilter;
8077
import org.assertj.core.api.filter.NotFilter;
@@ -92,7 +89,6 @@
9289
import org.assertj.core.description.Description;
9390
import org.assertj.core.groups.Properties;
9491
import org.assertj.core.groups.Tuple;
95-
import org.assertj.core.internal.Failures;
9692
import org.assertj.core.presentation.BinaryRepresentation;
9793
import org.assertj.core.presentation.HexadecimalRepresentation;
9894
import org.assertj.core.presentation.Representation;
@@ -1334,25 +1330,6 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> then(T actual)
13341330
return assertThat(catchThrowable(shouldRaiseThrowable)).hasBeenThrown();
13351331
}
13361332

1337-
/**
1338-
* Similar to {@link #thenThrownBy(ThrowingCallable)}, but when the called code returns a value instead of
1339-
* throwing, the assertion error shows the returned value to help understand what went wrong.
1340-
*
1341-
* @param shouldRaiseThrowable The {@link ThrowingCallableWithValue} or lambda with the code that should raise the throwable.
1342-
* @return the created {@link ThrowableAssert}.
1343-
* @since 3.25.0
1344-
*/
1345-
@CanIgnoreReturnValue
1346-
public static AbstractThrowableAssert<?, ? extends Throwable> thenThrownBy(ThrowingCallableWithValue shouldRaiseThrowable) {
1347-
Object value;
1348-
try {
1349-
value = shouldRaiseThrowable.call();
1350-
} catch (Throwable throwable) {
1351-
return assertThat(throwable);
1352-
}
1353-
throw Failures.instance().failure(format("Expecting code to raise a throwable, but it returned [%s] instead", value));
1354-
}
1355-
13561333
/**
13571334
* Allows to capture and then assert on a {@link Throwable} like {@code thenThrownBy(ThrowingCallable)} but this method
13581335
* let you set the assertion description the same way you do with {@link AbstractAssert#as(String, Object...) as(String, Object...)}.
@@ -1390,26 +1367,6 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> then(T actual)
13901367
return assertThat(catchThrowable(shouldRaiseThrowable)).as(description, args).hasBeenThrown();
13911368
}
13921369

1393-
/**
1394-
* Similar to {@link #thenThrownBy(ThrowingCallable, String, Object...)}, but when the called code returns a value instead of
1395-
* throwing, the assertion error shows the returned value to help understand what went wrong.
1396-
*
1397-
* @param shouldRaiseThrowable The {@link ThrowingCallableWithValue} or lambda with the code that should raise the throwable.
1398-
* @return the created {@link ThrowableAssert}.
1399-
* @since 3.25.0
1400-
*/
1401-
@CanIgnoreReturnValue
1402-
public static AbstractThrowableAssert<?, ? extends Throwable> thenThrownBy(ThrowingCallableWithValue shouldRaiseThrowable,
1403-
String description, Object... args) {
1404-
Object value;
1405-
try {
1406-
value = shouldRaiseThrowable.call();
1407-
} catch (Throwable throwable) {
1408-
return assertThat(throwable).as(description, args);
1409-
}
1410-
throw Failures.instance().failure(format("Expecting code to raise a throwable, but it returned [%s] instead", value));
1411-
}
1412-
14131370
/**
14141371
* Allows to capture and then assert on a {@link Throwable} more easily when used with Java 8 lambdas.
14151372
*

assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ public interface ThrowingCallable {
3333
void call() throws Throwable;
3434
}
3535

36-
public interface ThrowingCallableWithValue {
37-
Object call() throws Throwable;
38-
}
39-
4036
public ThrowableAssert(ACTUAL actual) {
4137
super(actual, ThrowableAssert.class);
4238
}

assertj-core/src/test/java/org/assertj/core/api/Assertions_assertThatThrownBy_Test.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
import static java.lang.String.format;
1616
import static org.assertj.core.api.Assertions.assertThatThrownBy;
17-
import static org.assertj.core.api.Assertions_catchThrowableOfType_Test.raisingException;
18-
import static org.assertj.core.api.Assertions_catchThrowable_Test.codeThrowing;
1917
import static org.assertj.core.api.BDDAssertions.then;
18+
import static org.assertj.core.error.ShouldHaveMessage.shouldHaveMessage;
2019
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
2120

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

2726
@Test
28-
void should_build_ThrowableAssert_with_runtime_exception_thrown() {
27+
void should_work_with_runtime_exception_thrown() {
28+
// WHEN/THEN
2929
assertThatThrownBy(codeThrowing(new IllegalArgumentException("boom"))).isInstanceOf(IllegalArgumentException.class)
3030
.hasMessage("boom");
3131
}
3232

3333
@Test
34-
void should_build_ThrowableAssert_with_throwable_thrown() {
34+
void should_work_with_throwable_thrown() {
35+
// WHEN/THEN
3536
assertThatThrownBy(codeThrowing(new Throwable("boom"))).isInstanceOf(Throwable.class)
3637
.hasMessage("boom");
3738
}
3839

3940
@Test
40-
void should_be_able_to_pass_a_description_to_assertThatThrownBy() {
41+
void should_work_with_method_reference_having_vararg_parameter() {
42+
// WHEN/THEN
43+
assertThatThrownBy(Assertions_assertThatThrownBy_Test::methodThrowing).isInstanceOf(Exception.class)
44+
.hasMessage("boom");
45+
}
46+
47+
@Test
48+
void should_support_description() {
4149
// GIVEN
42-
// make assertThatThrownBy fail to verify the description afterwards
43-
ThrowingCallable code = () -> assertThatThrownBy(raisingException("boom"), "Test %s", "code").hasMessage("bam");
50+
Throwable throwable = new Exception("boom");
4451
// WHEN
45-
AssertionError assertionError = expectAssertionError(code);
52+
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(codeThrowing(throwable), "Test %s",
53+
"code").hasMessage("bam"));
4654
// THEN
4755
then(assertionError).hasMessageContaining("[Test code]");
4856
}
4957

5058
@Test
5159
void should_fail_if_no_throwable_was_thrown() {
52-
// GIVEN
53-
ThrowingCallable code = () -> {};
5460
// WHEN
55-
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(code).hasMessage("boom ?"));
61+
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(() -> {}).hasMessage("boom ?"));
5662
// THEN
5763
then(assertionError).hasMessage(format("%nExpecting code to raise a throwable."));
5864
}
5965

6066
@Test
61-
void should_fail_with_good_message_when_assertion_is_failing() {
67+
void should_fail_with_proper_message_when_assertion_is_failing() {
68+
// GIVEN
69+
Throwable throwable = new Exception("boom");
6270
// WHEN
63-
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(raisingException("boom")).hasMessage("bam"));
71+
AssertionError assertionError = expectAssertionError(() -> assertThatThrownBy(codeThrowing(throwable)).hasMessage("bam"));
6472
// THEN
65-
then(assertionError).hasMessageContainingAll("Expecting message to be:",
66-
"\"bam\"",
67-
"but was:",
68-
"\"boom\"");
73+
then(assertionError).hasMessage(shouldHaveMessage(throwable, "bam").create());
74+
}
75+
76+
private static ThrowingCallable codeThrowing(Throwable t) {
77+
return () -> {
78+
throw t;
79+
};
80+
}
81+
82+
private static void methodThrowing(Object... parameters) throws Exception {
83+
throw new Exception("boom");
6984
}
7085

7186
}

assertj-core/src/test/java/org/assertj/core/api/throwable/ThrowableAssert_built_from_ThrowingCallable_Test.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
*/
1313
package org.assertj.core.api.throwable;
1414

15-
import static java.lang.String.format;
1615
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1716
import static org.assertj.core.api.Assertions.assertThatThrownBy;
18-
import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy;
1917

2018
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
2119
import org.junit.jupiter.api.Test;
@@ -52,14 +50,7 @@ public void call() {
5250
// no exception
5351
}
5452
});
55-
}).withMessage(format("%nExpecting code to raise a throwable."));
53+
}).withMessage(String.format("%nExpecting code to raise a throwable."));
5654
}
5755

58-
@Test
59-
void should_fail_and_show_value_returned_by_callable_code() {
60-
// GIVEN
61-
ThrowingCallable code = () -> assertThatThrownBy(() -> 42);
62-
// WHEN/THEN
63-
assertThatAssertionErrorIsThrownBy(code).withMessage("Expecting code to raise a throwable, but it returned [42] instead");
64-
}
6556
}

assertj-core/src/test/java/org/assertj/core/api/throwable/ThrowableAssert_built_with_then_method_Test.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1616
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
17-
import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy;
1817

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

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

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

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

0 commit comments

Comments
 (0)