-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathAssumptionExamples.java
136 lines (119 loc) · 5.11 KB
/
AssumptionExamples.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
/**
* 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.Assumptions.assumeThat;
import static org.assertj.core.api.Assumptions.assumeThatCode;
import static org.assertj.core.api.BDDAssumptions.given;
import static org.assertj.examples.data.Race.HOBBIT;
import static org.assertj.examples.data.Race.ORC;
import org.assertj.examples.data.TolkienCharacter;
import org.junit.AfterClass;
import org.junit.jupiter.api.Test;
public class AssumptionExamples extends AbstractAssertionsExamples {
// the data used are initialized in AbstractAssertionsExamples.
private static int ranTests = 0;
@AfterClass
public static void afterClass() {
assertThat(ranTests).as("number of tests run").isEqualTo(2);
}
@Test
public void when_assumption_is_not_met_the_test_should_be_ignored() {
// since this assumption is obviously false ...
assumeThat(frodo.getRace()).isEqualTo(ORC);
// ... this assertion should not be performed.
assertThat(fellowshipOfTheRing).contains(sauron);
fail("should not arrive here");
}
@Test
public void given_assumption_is_not_met_the_test_should_be_ignored() {
// BDD style assumptions
// since this assumption is obviously false ...
given(frodo.getRace()).isEqualTo(ORC);
// ... this assertion should not be performed.
assertThat(fellowshipOfTheRing).contains(sauron);
fail("should not arrive here");
}
@Test
public void when_string_comparable_assumption_is_not_met_the_test_should_be_ignored() {
// since this assumption is obviously false ...
assumeThat(frodo.getName()).isGreaterThan("Gandalf");
// ... this assertion should not be performed.
assertThat(fellowshipOfTheRing).contains(sauron);
fail("should not arrive here");
}
@Test
public void when_assumption_is_met_the_test_should_be_run() {
// since this assumption is true ...
assumeThat(frodo.getRace()).isEqualTo(HOBBIT);
// ... this assertion should be performed.
assertThat(fellowshipOfTheRing).doesNotContain(sauron);
ranTests++;
}
@Test
public void when_all_assumptions_are_met_the_test_should_be_run() {
// since this assumption is true ...
assumeThat(frodo.getRace()).isEqualTo(HOBBIT)
.isNotEqualTo(ORC);
// ... this assertion should be performed.
assertThat(fellowshipOfTheRing).doesNotContain(sauron);
ranTests++;
}
@Test
public void can_use_extracting_feature_in_assumptions() {
// since this assumption is obviously false ...
assumeThat(fellowshipOfTheRing).extracting("race")
.contains(ORC);
// ... this assertion should not be performed.
assertThat(fellowshipOfTheRing).contains(sauron);
fail("should not arrive here");
}
@Test
public void assumeThatCode_assumption_not_met_example() {
// since this assumption is obviously false ...
assumeThatCode(() -> fellowshipOfTheRing.get(1000)).doesNotThrowAnyException();
// ... this assertion should not be performed.
assertThat(fellowshipOfTheRing).contains(sauron);
fail("should not arrive here");
}
@Test
public void assumeThatCode_assumption_met_example() {
// since the given code throws an ArrayIndexOutOfBoundsException ...
assumeThatCode(() -> fellowshipOfTheRing.get(1000)).isInstanceOf(IndexOutOfBoundsException.class);
// ... this assertion should be performed.
assertThat(fellowshipOfTheRing).contains(frodo);
}
@Test
public void all_assumptions_must_be_met_otherwise_the_test_is_ignored() {
// since one of the assumptions is obviously false ...
assumeThat(frodo.getRace()).isEqualTo(HOBBIT);
assumeThat(fellowshipOfTheRing).extracting("race")
.contains(ORC);
// ... this assertion should not be performed.
assertThat(fellowshipOfTheRing).contains(sauron);
fail("should not arrive here");
}
@Test
public void assumptions_still_work_when_switching_the_object_under_test() {
assumeThat(fellowshipOfTheRing).size()
.isLessThan(100)
.returnToIterable()
.filteredOn("race", HOBBIT)
.containsOnly(sam, frodo, pippin, merry)
.extracting(TolkienCharacter::getName)
.contains("Frodo")
.contains("Elrond"); // fail
fail("should not arrive here");
}
}