Skip to content

Commit 6d65477

Browse files
Add support for assumptions
1 parent fc29442 commit 6d65477

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import java.util.stream.Stream;
6868

6969
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
70+
import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
7071
import org.assertj.core.util.CheckReturnValue;
7172

7273
import net.bytebuddy.ByteBuddy;
@@ -1181,11 +1182,19 @@ private static RuntimeException assumptionNotMet(Class<?> exceptionClass,
11811182
if (assertion instanceof MapSizeAssert) return asMapSizeAssumption(assertion);
11821183
if (assertion instanceof ProxyableObjectAssert) return asAssumption(ObjectAssert.class, Object.class, actual);
11831184
if (assertion instanceof ObjectAssert) return asAssumption(ObjectAssert.class, Object.class, actual);
1185+
if (assertion instanceof RecursiveComparisonAssert) return asRecursiveComparisonAssumption(assertion);
11841186
// @format:on
11851187
// should not arrive here
11861188
throw new IllegalArgumentException("Unsupported assumption creation for " + assertion.getClass());
11871189
}
11881190

1191+
private static AbstractAssert<?, ?> asRecursiveComparisonAssumption(AbstractAssert<?, ?> assertion) {
1192+
RecursiveComparisonAssert<?> recursiveComparisonAssert = (RecursiveComparisonAssert<?>) assertion;
1193+
RecursiveComparisonConfiguration recursiveComparisonConfiguration = recursiveComparisonAssert.getRecursiveComparisonConfiguration();
1194+
Class<?>[] constructorTypes = array(Object.class, RecursiveComparisonConfiguration.class);
1195+
return asAssumption(RecursiveComparisonAssert.class, constructorTypes, assertion.actual, recursiveComparisonConfiguration);
1196+
}
1197+
11891198
private static AbstractAssert<?, ?> asMapSizeAssumption(AbstractAssert<?, ?> assertion) {
11901199
MapSizeAssert<?, ?> mapSizeAssert = (MapSizeAssert<?, ?>) assertion;
11911200
Class<?>[] constructorTypes = array(AbstractMapAssert.class, Integer.class);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2018 the original author or authors.
12+
*/
13+
package org.assertj.core.api.recursive.comparison;
14+
15+
import static org.assertj.core.api.Assertions.assertThatCode;
16+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
17+
import static org.assertj.core.api.Assumptions.assumeThat;
18+
import static org.assertj.core.presentation.UnicodeRepresentation.UNICODE_REPRESENTATION;
19+
20+
import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;
21+
import org.assertj.core.internal.objects.data.Person;
22+
import org.junit.AssumptionViolatedException;
23+
import org.junit.jupiter.api.Test;
24+
25+
public class RecursiveComparisonAssert_assumptions_Test extends RecursiveComparisonAssert_isEqualTo_BaseTest {
26+
27+
@Test
28+
public void should_ignore_test_when_one_of_the_assumption_fails() {
29+
// GIVEN
30+
Person actual = new Person("John");
31+
actual.home.address.number = 1;
32+
Person expected = new Person("John");
33+
expected.home.address.number = 1;
34+
Person unexpected = new Person("John");
35+
unexpected.home.address.number = 2;
36+
// THEN
37+
assumeThat(actual).usingRecursiveComparison().isEqualTo(expected);
38+
assertThatExceptionOfType(AssumptionViolatedException.class).isThrownBy(() -> assumeThat(actual).usingRecursiveComparison()
39+
.isEqualTo(unexpected));
40+
}
41+
42+
@Test
43+
public void should_run_test_when_all_assumptions_are_met() {
44+
// GIVEN
45+
Person actual = new Person("John");
46+
actual.home.address.number = 1;
47+
Person expected = new Person("John");
48+
expected.home.address.number = 1;
49+
// THEN
50+
assertThatCode(() -> {
51+
assumeThat("foo").isNotNull()
52+
.isNotEmpty()
53+
.isEqualTo("foo");
54+
assumeThat(actual).usingRecursiveComparison().isEqualTo(expected);
55+
assumeThat(expected).usingRecursiveComparison().isEqualTo(actual);
56+
assumeThat(actual).as("test description")
57+
.withFailMessage("error message")
58+
.withRepresentation(UNICODE_REPRESENTATION)
59+
.usingRecursiveComparison()
60+
.isEqualTo(expected);
61+
}).doesNotThrowAnyException();
62+
}
63+
64+
}

0 commit comments

Comments
 (0)