-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ClassLevelCallbackTests.java
143 lines (114 loc) · 4.27 KB
/
ClassLevelCallbackTests.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
/*
* Copyright 2015-2016 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 v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.gen5.engine.junit5;
import static java.util.Arrays.asList;
import static org.junit.gen5.api.Assertions.assertEquals;
import static org.junit.gen5.engine.TestPlanSpecification.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.gen5.api.AfterAll;
import org.junit.gen5.api.BeforeAll;
import org.junit.gen5.api.Test;
import org.junit.gen5.api.extension.AfterAllExtensionPoint;
import org.junit.gen5.api.extension.BeforeAllExtensionPoint;
import org.junit.gen5.api.extension.ContainerExtensionContext;
import org.junit.gen5.api.extension.ExtendWith;
import org.junit.gen5.api.extension.ExtensionRegistrar;
import org.junit.gen5.api.extension.ExtensionRegistry;
import org.junit.gen5.engine.ExecutionEventRecordingEngineExecutionListener;
import org.junit.gen5.engine.TestPlanSpecification;
/**
* Integration tests that verify support of {@link BeforeAll}, {@link AfterAll},
* and {@link BeforeAllExtensionPoint} in the {@link JUnit5TestEngine}.
*
* @since 5.0
*/
public class ClassLevelCallbackTests extends AbstractJUnit5TestEngineTests {
@Test
public void beforeAllAndAfterAllCallbacks() {
TestPlanSpecification spec = build(forClass(InstancePerMethodTestCase.class));
ExecutionEventRecordingEngineExecutionListener eventRecorder = executeTests(spec);
assertEquals(1L, eventRecorder.getTestStartedCount(), "# tests started");
assertEquals(1L, eventRecorder.getTestSuccessfulCount(), "# tests succeeded");
// @formatter:off
assertEquals(asList(
"outermostBefore",
"fooBeforeAll",
"barBeforeAll",
"beforeAllMethod",
"innermostBefore",
"firstTest",
"innermostAfter",
"afterAllMethod",
"barAfterAll",
"fooAfterAll",
"outermostAfter"
), callSequence, "wrong call sequence");
// @formatter:on
}
// -------------------------------------------------------------------
private static List<String> callSequence = new ArrayList<>();
@ExtendWith({ FooClassLevelCallbacks.class, BarClassLevelCallbacks.class, InnermostAndOutermost.class })
private static class InstancePerMethodTestCase {
@BeforeAll
static void beforeAll() {
callSequence.add("beforeAllMethod");
}
@AfterAll
static void afterAll() {
callSequence.add("afterAllMethod");
}
@Test
void firstTest() {
callSequence.add("firstTest");
}
}
private static class InnermostAndOutermost implements ExtensionRegistrar {
@Override
public void registerExtensions(ExtensionRegistry registry) {
registry.register(this::innermostBefore, BeforeAllExtensionPoint.class, Position.INNERMOST);
registry.register(this::innermostAfter, AfterAllExtensionPoint.class, Position.INNERMOST);
registry.register(this::outermostBefore, BeforeAllExtensionPoint.class, Position.OUTERMOST);
registry.register(this::outermostAfter, AfterAllExtensionPoint.class, Position.OUTERMOST);
}
private void outermostBefore(ContainerExtensionContext context) {
callSequence.add("outermostBefore");
}
private void innermostBefore(ContainerExtensionContext context) {
callSequence.add("innermostBefore");
}
private void outermostAfter(ContainerExtensionContext context) {
callSequence.add("outermostAfter");
}
private void innermostAfter(ContainerExtensionContext context) {
callSequence.add("innermostAfter");
}
}
private static class FooClassLevelCallbacks implements BeforeAllExtensionPoint, AfterAllExtensionPoint {
@Override
public void beforeAll(ContainerExtensionContext testExecutionContext) {
callSequence.add("fooBeforeAll");
}
@Override
public void afterAll(ContainerExtensionContext testExecutionContext) {
callSequence.add("fooAfterAll");
}
}
private static class BarClassLevelCallbacks implements BeforeAllExtensionPoint, AfterAllExtensionPoint {
@Override
public void beforeAll(ContainerExtensionContext testExecutionContext) {
callSequence.add("barBeforeAll");
}
@Override
public void afterAll(ContainerExtensionContext testExecutionContext) {
callSequence.add("barAfterAll");
}
}
}