forked from assertj/assertj-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoftAssertionsExamples.java
366 lines (329 loc) · 13.3 KB
/
SoftAssertionsExamples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.examples;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.examples.data.Race.HOBBIT;
import static org.assertj.examples.data.Ring.oneRing;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.assertj.core.api.AutoCloseableBDDSoftAssertions;
import org.assertj.core.api.AutoCloseableSoftAssertions;
import org.assertj.core.api.BDDSoftAssertions;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.util.Arrays;
import org.assertj.examples.custom.MyProjectAssertions;
import org.assertj.examples.data.Mansion;
import org.assertj.examples.data.TolkienCharacter;
import org.junit.jupiter.api.Test;
public class SoftAssertionsExamples extends AbstractAssertionsExamples {
private Mansion mansion = new Mansion();
@Test
public void host_dinner_party_where_nobody_dies() {
mansion.hostPotentiallyMurderousDinnerParty();
SoftAssertions softly = new SoftAssertions();
softly.assertThat(mansion.guests()).as("Living Guests").isEqualTo(7);
softly.assertThat(mansion.kitchen()).as("Kitchen").isEqualTo("clean");
softly.assertThat(mansion.library()).as("Library").isEqualTo("clean");
softly.assertThat(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6);
softly.assertThat(mansion.candlestick()).as("Candlestick").isEqualTo("pristine");
softly.assertThat(mansion.colonel()).as("Colonel").isEqualTo("well kempt");
softly.assertThat(mansion.professor()).as("Professor").isEqualTo("well kempt");
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("SoftAssertion errors example", e);
}
}
@Test
public void lotr_soft_assertions_example() {
SoftAssertions softly = new SoftAssertions();
softly.assertThat(frodo.getRace().getName()).as("check Frodo's race").isEqualTo("Orc");
softly.assertThat(aragorn.age).as("check Aragorn's age").isGreaterThan(500);
softly.assertThat(gandalf).as("gandalf vs sauron").isEqualTo(sauron);
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("SoftAssertion errors example", e);
}
}
@Test
public void lotr_soft_assertions_example_with_assertSoftly() {
try {
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(frodo.getRace().getName()).as("check Frodo's race").isEqualTo("Orc");
softly.assertThat(aragorn.age).as("check Aragorn's age").isGreaterThan(500);
softly.assertThat(gandalf).as("gandalf vs sauron").isEqualTo(sauron);
});
} catch (AssertionError e) {
// expected
}
}
@Test
public void chained_soft_assertions_example() {
String name = "Michael Jordan - Bulls";
SoftAssertions softly = new SoftAssertions();
softly.assertThat(name)
.isLessThan("Lebron")
.startsWith("Mike")
.contains("Lakers")
.endsWith("Chicago");
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("SoftAssertion errors example", e);
}
}
@Test
public void auto_closed_host_dinner_party_where_nobody_dies() {
Mansion mansion = new Mansion();
mansion.hostPotentiallyMurderousDinnerParty();
try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
softly.assertThat(mansion.guests()).as("Living Guests").isEqualTo(7);
softly.assertThat(mansion.kitchen()).as("Kitchen").isEqualTo("clean");
softly.assertThat(mansion.library()).as("Library").isEqualTo("clean");
softly.assertThat(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6);
softly.assertThat(mansion.candlestick()).as("Candlestick").isEqualTo("pristine");
softly.assertThat(mansion.colonel()).as("Colonel").isEqualTo("well kempt");
softly.assertThat(mansion.professor()).as("Professor").isEqualTo("well kempt");
} catch (AssertionError e) {
// expected
return;
}
fail("AssertionError expected.");
}
// same test but for BDD style soft assertions
@Test
public void host_dinner_party_where_nobody_dies_bdd_style() {
Mansion mansion = new Mansion();
mansion.hostPotentiallyMurderousDinnerParty();
BDDSoftAssertions softly = new BDDSoftAssertions();
softly.then(mansion.guests()).as("Living Guests").isEqualTo(7);
softly.then(mansion.kitchen()).as("Kitchen").isEqualTo("clean");
softly.then(mansion.library()).as("Library").isEqualTo("clean");
softly.then(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6);
softly.then(mansion.candlestick()).as("Candlestick").isEqualTo("pristine");
softly.then(mansion.colonel()).as("Colonel").isEqualTo("well kempt");
softly.then(mansion.professor()).as("Professor").isEqualTo("well kempt");
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("BDD SoftAssertion errors example", e);
}
}
@Test
public void chained_bdd_soft_assertions_example() {
String name = "Michael Jordan - Bulls";
BDDSoftAssertions softly = new BDDSoftAssertions();
softly.then(name).startsWith("Mike").contains("Lakers").endsWith("Chicago");
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("BDD SoftAssertion errors example", e);
}
}
@Test
public void auto_closed_host_dinner_party_where_nobody_dies_bdd_style() {
Mansion mansion = new Mansion();
mansion.hostPotentiallyMurderousDinnerParty();
try (AutoCloseableBDDSoftAssertions softly = new AutoCloseableBDDSoftAssertions()) {
softly.then(mansion.guests()).as("Living Guests").isEqualTo(7);
softly.then(mansion.kitchen()).as("Kitchen").isEqualTo("clean");
softly.then(mansion.library()).as("Library").isEqualTo("clean");
softly.then(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6);
softly.then(mansion.candlestick()).as("Candlestick").isEqualTo("pristine");
softly.then(mansion.colonel()).as("Colonel").isEqualTo("well kempt");
softly.then(mansion.professor()).as("Professor").isEqualTo("well kempt");
} catch (AssertionError e) {
// expected
return;
}
fail("BDD SoftAssertionError expected.");
}
@Test
public void soft_assertions_combined_with_extracting_example() {
BDDSoftAssertions softly = new BDDSoftAssertions();
softly.then(fellowshipOfTheRing).extracting("name", "age").contains(tuple("Sauron", 1000));
softly.then(fellowshipOfTheRing).extracting("race.name").contains("Man", "Orc");
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("BDD SoftAssertion errors example", e);
}
}
@Test
public void soft_assertions_combined_with_filtering_example() {
BDDSoftAssertions softly = new BDDSoftAssertions();
softly.then(fellowshipOfTheRing).filteredOn("name", "Frodo").containsOnly(frodo);
softly.then(fellowshipOfTheRing).filteredOn("name", "Frodo").isEmpty();
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("BDD SoftAssertion errors example", e);
return;
}
throw new AssertionError("should have caught soft assertion errors properly");
}
@Test
public void soft_assertions_example_with_arrays() {
String[] players = Arrays.array("Michael Jordan", "Tim Duncan");
BDDSoftAssertions softly = new BDDSoftAssertions();
softly.then(players).contains("Kobe Bryant").doesNotContain("Tim Duncan");
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("BDD SoftAssertion errors example", e);
}
}
@Test
public void should_work_with_comparable() throws Exception {
SoftAssertions softly = new SoftAssertions();
Example example = new Example(0);
softly.assertThat((Object) example).isEqualTo(example);
softly.assertAll();
}
@Test
public void should_return_correct_errors_count() {
SoftAssertions soft = new SoftAssertions();
soft.assertThat("foo").startsWith("boo");
assertThat(soft.errorsCollected()).hasSize(1);
soft.assertThat("bar").startsWith("far");
assertThat(soft.errorsCollected()).hasSize(2);
}
@Test
public void bug_1146() {
// GIVEN
Map<String, String> data = new HashMap<>();
data.put("one", "1");
data.put("two", "2");
data.put("three", "3");
// THEN
try (final AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
softly.assertThat(data)
.extracting("one")
.isEqualTo("1");
}
}
@SuppressWarnings("unchecked")
@Test
public void should_work_with_methods_switching_the_object_under_test() {
// GIVEN
SoftAssertions softly = new SoftAssertions();
Object sneakyString = "I'm actually a String";
Object sneakyList = fellowshipOfTheRing;
// WHEN
softly.assertThat(fellowshipOfTheRing)
.flatExtracting(TolkienCharacter::getName, tc -> tc.getRace().getName())
.contains("Hobbit", "Frodo", "Elf", "Legolas")
.contains("Sauron"); // fail
softly.assertThat(fellowshipOfTheRing)
.size()
.isLessThan(100)
.isGreaterThan(100) // fail
.returnToIterable()
.filteredOn("race", HOBBIT)
.containsOnly(sam, frodo, pippin, merry)
.contains(gandalf) // fail
.extracting(TolkienCharacter::getName)
.contains("Frodo")
.contains("Elrond"); // fail
softly.assertThat(ringBearers)
.size()
.isLessThan(10)
.isGreaterThan(10) // fail
.returnToMap()
.containsKey(oneRing)
.containsValue(aragorn); // fail
softly.assertThat(Optional.of("Yoda"))
.flatMap(s -> s == null ? Optional.empty() : Optional.of(s.toUpperCase()))
.contains("YODA")
.contains("yoda") // fail
.map(String::length)
.hasValue(4)
.hasValue(777); // fail
softly.assertThat(sneakyString)
.asString()
.endsWith("ing")
.contains("oh no"); // fail
softly.assertThat(sneakyList)
.asList()
.flatExtracting("name", "race.name")
.contains("Hobbit", "Frodo", "Elf", "Legolas")
.contains("Saruman"); // fail
// THEN
List<Throwable> errors = softly.errorsCollected();
assertThat(errors).hasSize(10);
assertThat(errors.get(0)).hasMessageContaining("Sauron");
assertThat(errors.get(1)).hasMessageContaining("100");
assertThat(errors.get(2)).hasMessageContaining("Gandalf");
assertThat(errors.get(3)).hasMessageContaining("Elrond");
assertThat(errors.get(4)).hasMessageContaining("10");
assertThat(errors.get(5)).hasMessageContaining("Aragorn");
assertThat(errors.get(6)).hasMessageContaining("yoda");
assertThat(errors.get(7)).hasMessageContaining("777");
assertThat(errors.get(8)).hasMessageContaining("oh no");
assertThat(errors.get(9)).hasMessageContaining("Saruman");
}
@Test
void testName() throws Exception {
// GIVEN
SoftAssertions softly = new SoftAssertions();
// WHEN
softly.check(() -> MyProjectAssertions.assertThat(frodo).hasName("Frodon"));
softly.check(() -> MyProjectAssertions.assertThat(frodo).hasName("Frodo"));
// THEN
assertThat(softly.errorsCollected()).hasSize(1);
}
private SoftAssertions check_kitchen() {
SoftAssertions softly = new SoftAssertions();
softly.assertThat(mansion.kitchen()).as("Kitchen").isEqualTo("clean");
return softly;
}
private SoftAssertions check_library() {
SoftAssertions softly = new SoftAssertions();
softly.assertThat(mansion.library()).as("Library").isEqualTo("clean");
return softly;
}
@Test
void combining_different_soft_assertions_instances_with_assertAlso_example() {
SoftAssertions softly = new SoftAssertions();
mansion.hostPotentiallyMurderousDinnerParty();
softly.assertThat(mansion.guests()).as("Living Guests").isEqualTo(7);
softly.assertThat(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6);
softly.assertThat(mansion.candlestick()).as("Candlestick").isEqualTo("pristine");
softly.assertThat(mansion.colonel()).as("Colonel").isEqualTo("well kempt");
softly.assertThat(mansion.professor()).as("Professor").isEqualTo("well kempt");
SoftAssertions kitchen = check_kitchen();
softly.assertAlso(kitchen);
SoftAssertions library = check_library();
softly.assertAlso(library);
try {
softly.assertAll();
} catch (AssertionError e) {
logAssertionErrorMessage("SoftAssertion errors example", e);
}
}
class Example implements Comparable<Example> {
int id;
Example(int id) {
this.id = id;
}
@Override
public int compareTo(Example that) {
return this.id - that.id;
}
}
}