Skip to content

Commit 4f6d0c7

Browse files
gmullerbjoel-costigliola
authored andcommitted
Adds and static field to BDDAssertions.
Fixes #1640
1 parent 9f597c9 commit 4f6d0c7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@
8282
*
8383
* then(bulls).contains(rose, noah).doesNotContain(james);
8484
* }</code></pre>
85+
* Use <code>{@link #and}</code> to avoid clash with other libraries (like BDDMockito) exposing '<code>then(object)</code>'.
86+
* You might have to ignore a warning like: <i>The static method BDDAssertions.then() should be accessed in a static way</i>.
8587
*
88+
*
89+
* @author Gonzalo Müller
8690
* @author Alex Ruiz
8791
* @author Yvonne Wang
8892
* @author David DIDIER
@@ -103,6 +107,48 @@ public class BDDAssertions extends Assertions {
103107
*/
104108
protected BDDAssertions() {}
105109

110+
/**
111+
* A <code>BDDAssertions</code> which allows to blend assertions with other libraries when the name '<code>then</code>' cause clash.
112+
* <p>
113+
* Examples:
114+
* <pre><code class='java'> import static org.assertj.core.api.BDDAssertions.and;
115+
* import static org.mockito.BDDMockito.then;
116+
* import static org.mockito.Mockito.mock;
117+
* import static org.mockito.Mockito.times;
118+
*
119+
* // suppress and.then warning: The static method BDDAssertions.then() should be accessed in a static way
120+
* {@literal @SuppressWarnings}("static-access")
121+
* {@literal @Test}
122+
* public void bdd_assertions_with_bdd_mockito() {
123+
* // GIVEN
124+
* Person person = mock(Person.class)
125+
* // WHEN
126+
* person.ride(bike);
127+
* person.ride(bike);
128+
* // THEN
129+
* // mockito then()
130+
* then(person).should(times(2)).ride(bike);
131+
* // use AssertJ and.then(person) as then(person) would clash with mockito then(person)
132+
* and.then(person.hasBike()).isTrue();
133+
* }</code></pre>
134+
* <p>
135+
* Can also be used to add extra readability:
136+
* <pre><code class='java'> import static org.assertj.core.api.BDDAssertions.and;
137+
* import static org.assertj.core.api.BDDAssertions.then;
138+
*
139+
* // suppress and.then warning: The static method BDDAssertions.then() should be accessed in a static way
140+
* {@literal @SuppressWarnings}("static-access")
141+
* {@literal @Test}
142+
* public void bdd_assertions_with_and_field() {
143+
* // ...
144+
* then(person.hasBike()).isTrue()
145+
* and.then(bike.isNew()).isFalse();
146+
* }</code></pre>
147+
*
148+
* @since 3.14.0
149+
*/
150+
public static final BDDAssertions and = new BDDAssertions();
151+
106152
/**
107153
* Create assertion for {@link Predicate}.
108154
*

src/test/java/org/assertj/core/api/BDDAssertions_then_Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import static java.util.Arrays.asList;
1616
import static java.util.Collections.singletonList;
17+
import static org.assertj.core.api.BDDAssertions.and;
1718
import static org.assertj.core.api.BDDAssertions.then;
1819
import static org.assertj.core.api.BDDAssertions.thenObject;
1920
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
@@ -281,4 +282,10 @@ public void then_Spliterator() {
281282
Spliterator<Integer> spliterator = Stream.of(1, 2).spliterator();
282283
then(spliterator).hasCharacteristics(Spliterator.SIZED);
283284
}
285+
286+
@Test
287+
public void and_then() {
288+
and.then(true).isNotEqualTo(false);
289+
and.then(1L).isEqualTo(1L);
290+
}
284291
}

0 commit comments

Comments
 (0)