forked from junit-team/junit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestClassInheritanceTests.java
More file actions
285 lines (231 loc) · 7.87 KB
/
Copy pathTestClassInheritanceTests.java
File metadata and controls
285 lines (231 loc) · 7.87 KB
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
/*
* Copyright 2015-2025 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.jupiter.engine;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.testkit.engine.EngineExecutionResults;
/**
* Integration tests for test class hierarchy support in the {@link JupiterTestEngine}.
*
* @since 5.0
*/
class TestClassInheritanceTests extends AbstractJupiterTestEngineTests {
private static final List<String> callSequence = new ArrayList<>();
@BeforeEach
void initStatics() {
callSequence.clear();
LocalTestCase.countBeforeInvoked = 0;
LocalTestCase.countAfterInvoked = 0;
AbstractTestCase.countSuperBeforeInvoked = 0;
AbstractTestCase.countSuperAfterInvoked = 0;
}
@Test
void executeAllTestsInClass() {
EngineExecutionResults executionResults = executeTestsForClass(LocalTestCase.class);
assertEquals(6, executionResults.testEvents().started().count(), "# tests started");
assertEquals(3, executionResults.testEvents().succeeded().count(), "# tests succeeded");
assertEquals(0, executionResults.testEvents().skipped().count(), "# tests skipped");
assertEquals(1, executionResults.testEvents().aborted().count(), "# tests aborted");
assertEquals(2, executionResults.testEvents().failed().count(), "# tests failed");
assertEquals(6, LocalTestCase.countBeforeInvoked, "# before calls");
assertEquals(6, LocalTestCase.countAfterInvoked, "# after calls");
assertEquals(6, AbstractTestCase.countSuperBeforeInvoked, "# super before calls");
assertEquals(6, AbstractTestCase.countSuperAfterInvoked, "# super after calls");
}
@Test
void executeSingleTest() {
EngineExecutionResults executionResults = executeTests(selectMethod(LocalTestCase.class, "alwaysPasses"));
assertEquals(1, executionResults.testEvents().started().count(), "# tests started");
assertEquals(1, executionResults.testEvents().succeeded().count(), "# tests succeeded");
assertEquals(0, executionResults.testEvents().skipped().count(), "# tests skipped");
assertEquals(0, executionResults.testEvents().aborted().count(), "# tests aborted");
assertEquals(0, executionResults.testEvents().failed().count(), "# tests failed");
}
@Test
void executeTestDeclaredInSuperClass() {
EngineExecutionResults executionResults = executeTests(selectMethod(LocalTestCase.class, "superTest"));
assertEquals(1, executionResults.testEvents().started().count(), "# tests started");
assertEquals(1, executionResults.testEvents().succeeded().count(), "# tests succeeded");
assertEquals(0, executionResults.testEvents().skipped().count(), "# tests skipped");
assertEquals(0, executionResults.testEvents().aborted().count(), "# tests aborted");
assertEquals(0, executionResults.testEvents().failed().count(), "# tests failed");
assertEquals(1, LocalTestCase.countBeforeInvoked, "# after calls");
assertEquals(1, LocalTestCase.countAfterInvoked, "# after calls");
assertEquals(1, AbstractTestCase.countSuperBeforeInvoked, "# super before calls");
assertEquals(1, AbstractTestCase.countSuperAfterInvoked, "# super after calls");
}
@Test
void executeTestWithExceptionThrownInAfterMethod() {
EngineExecutionResults executionResults = executeTests(
selectMethod(LocalTestCase.class, "throwExceptionInAfterMethod"));
assertEquals(1, executionResults.testEvents().started().count(), "# tests started");
assertEquals(0, executionResults.testEvents().succeeded().count(), "# tests succeeded");
assertEquals(0, executionResults.testEvents().skipped().count(), "# tests skipped");
assertEquals(0, executionResults.testEvents().aborted().count(), "# tests aborted");
assertEquals(1, executionResults.testEvents().failed().count(), "# tests failed");
}
@Test
void beforeAndAfterMethodsInTestClassHierarchy() {
EngineExecutionResults executionResults = executeTestsForClass(TestCase3.class);
// @formatter:off
assertAll(
() -> assertEquals(1, executionResults.testEvents().started().count(), "# tests started"),
() -> assertEquals(1, executionResults.testEvents().succeeded().count(), "# tests succeeded"),
() -> assertEquals(0, executionResults.testEvents().skipped().count(), "# tests skipped"),
() -> assertEquals(0, executionResults.testEvents().aborted().count(), "# tests aborted"),
() -> assertEquals(0, executionResults.testEvents().failed().count(), "# tests failed")
);
// @formatter:on
// @formatter:off
assertEquals(callSequence, asList(
"beforeAll1",
"beforeAll2",
"beforeAll3",
"beforeEach1",
"beforeEach2",
"beforeEach3",
"test3",
"afterEach3",
"afterEach2",
"afterEach1",
"afterAll3",
"afterAll2",
"afterAll1"
), "wrong call sequence");
// @formatter:on
}
// -------------------------------------------------------------------
private static abstract class AbstractTestCase {
static int countSuperBeforeInvoked = 0;
static int countSuperAfterInvoked = 0;
@BeforeEach
void superBefore() {
countSuperBeforeInvoked++;
}
@AfterEach
void superAfter() {
countSuperAfterInvoked++;
}
@Test
void superTest() {
/* no-op */
}
}
@SuppressWarnings("JUnitMalformedDeclaration")
static class LocalTestCase extends AbstractTestCase {
boolean throwExceptionInAfterMethod = false;
static int countBeforeInvoked = 0;
static int countAfterInvoked = 0;
@BeforeEach
void before() {
countBeforeInvoked++;
// Reset state, since the test instance is retained across all test methods;
// otherwise, after() always throws an exception.
this.throwExceptionInAfterMethod = false;
}
@AfterEach
void after() {
countAfterInvoked++;
if (this.throwExceptionInAfterMethod) {
throw new RuntimeException("Exception thrown from @AfterEach method");
}
}
@Test
void otherTest() {
/* no-op */
}
@Test
void throwExceptionInAfterMethod() {
this.throwExceptionInAfterMethod = true;
}
@Test
void alwaysPasses() {
/* no-op */
}
@Test
void aborted() {
assumeTrue(false);
}
@Test
void alwaysFails() {
fail("#fail");
}
}
static class TestCase1 {
@BeforeAll
static void beforeAll1() {
callSequence.add("beforeAll1");
}
@BeforeEach
void beforeEach1() {
callSequence.add("beforeEach1");
}
@AfterEach
void afterEach1() {
callSequence.add("afterEach1");
}
@AfterAll
static void afterAll1() {
callSequence.add("afterAll1");
}
}
static class TestCase2 extends TestCase1 {
@BeforeAll
static void beforeAll2() {
callSequence.add("beforeAll2");
}
@BeforeEach
void beforeEach2() {
callSequence.add("beforeEach2");
}
@AfterEach
void afterEach2() {
callSequence.add("afterEach2");
}
@AfterAll
static void afterAll2() {
callSequence.add("afterAll2");
}
}
@SuppressWarnings("JUnitMalformedDeclaration")
static class TestCase3 extends TestCase2 {
@BeforeAll
static void beforeAll3() {
callSequence.add("beforeAll3");
}
@BeforeEach
void beforeEach3() {
callSequence.add("beforeEach3");
}
@Test
void test3() {
callSequence.add("test3");
}
@AfterEach
void afterEach3() {
callSequence.add("afterEach3");
}
@AfterAll
static void afterAll3() {
callSequence.add("afterAll3");
}
}
}