/
TestExecutionExceptionHandlerTests.java
217 lines (172 loc) · 7.83 KB
/
TestExecutionExceptionHandlerTests.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
/*
* Copyright 2015-2024 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.extension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;
import static org.junit.platform.testkit.engine.EventConditions.container;
import static org.junit.platform.testkit.engine.EventConditions.engine;
import static org.junit.platform.testkit.engine.EventConditions.event;
import static org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully;
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
import static org.junit.platform.testkit.engine.EventConditions.started;
import static org.junit.platform.testkit.engine.EventConditions.test;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
import org.junit.jupiter.engine.AbstractJupiterTestEngineTests;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.testkit.engine.EngineExecutionResults;
/**
* Integration tests that verify support for {@link TestExecutionExceptionHandler}.
*
* @since 5.0
*/
class TestExecutionExceptionHandlerTests extends AbstractJupiterTestEngineTests {
static List<String> handlerCalls = new ArrayList<>();
@BeforeEach
void resetStatics() {
handlerCalls.clear();
RethrowException.handleExceptionCalled = false;
ConvertException.handleExceptionCalled = false;
SwallowException.handleExceptionCalled = false;
ShouldNotBeCalled.handleExceptionCalled = false;
}
@Test
void exceptionHandlerRethrowsException() {
LauncherDiscoveryRequest request = request().selectors(selectMethod(ATestCase.class, "testRethrow")).build();
EngineExecutionResults executionResults = executeTests(request);
assertTrue(RethrowException.handleExceptionCalled, "TestExecutionExceptionHandler should have been called");
executionResults.allEvents().assertEventsMatchExactly( //
event(engine(), started()), //
event(container(ATestCase.class), started()), //
event(test("testRethrow"), started()), //
event(test("testRethrow"), finishedWithFailure(instanceOf(IOException.class), message("checked"))), //
event(container(ATestCase.class), finishedSuccessfully()), //
event(engine(), finishedSuccessfully()));
}
@Test
void exceptionHandlerSwallowsException() {
LauncherDiscoveryRequest request = request().selectors(selectMethod(ATestCase.class, "testSwallow")).build();
EngineExecutionResults executionResults = executeTests(request);
assertTrue(SwallowException.handleExceptionCalled, "TestExecutionExceptionHandler should have been called");
executionResults.allEvents().assertEventsMatchExactly( //
event(engine(), started()), //
event(container(ATestCase.class), started()), //
event(test("testSwallow"), started()), //
event(test("testSwallow"), finishedSuccessfully()), //
event(container(ATestCase.class), finishedSuccessfully()), //
event(engine(), finishedSuccessfully()));
}
@Test
void exceptionHandlerConvertsException() {
LauncherDiscoveryRequest request = request().selectors(selectMethod(ATestCase.class, "testConvert")).build();
EngineExecutionResults executionResults = executeTests(request);
assertTrue(ConvertException.handleExceptionCalled, "TestExecutionExceptionHandler should have been called");
executionResults.allEvents().assertEventsMatchExactly( //
event(engine(), started()), //
event(container(ATestCase.class), started()), //
event(test("testConvert"), started()), //
event(test("testConvert"), finishedWithFailure(instanceOf(IOException.class), message("checked"))), //
event(container(ATestCase.class), finishedSuccessfully()), //
event(engine(), finishedSuccessfully()));
}
@Test
void severalHandlersAreCalledInOrder() {
LauncherDiscoveryRequest request = request().selectors(selectMethod(ATestCase.class, "testSeveral")).build();
EngineExecutionResults executionResults = executeTests(request);
assertTrue(ConvertException.handleExceptionCalled, "ConvertException should have been called");
assertTrue(RethrowException.handleExceptionCalled, "RethrowException should have been called");
assertTrue(SwallowException.handleExceptionCalled, "SwallowException should have been called");
assertFalse(ShouldNotBeCalled.handleExceptionCalled, "ShouldNotBeCalled should not have been called");
executionResults.allEvents().assertEventsMatchExactly( //
event(engine(), started()), //
event(container(ATestCase.class), started()), //
event(test("testSeveral"), started()), //
event(test("testSeveral"), finishedSuccessfully()), //
event(container(ATestCase.class), finishedSuccessfully()), //
event(engine(), finishedSuccessfully()));
assertEquals(Arrays.asList("convert", "rethrow", "swallow"), handlerCalls);
}
// -------------------------------------------------------------------
static class ATestCase {
@Test
@ExtendWith(RethrowException.class)
void testRethrow() throws IOException {
throw new IOException("checked");
}
@Test
@ExtendWith(SwallowException.class)
void testSwallow() throws IOException {
throw new IOException("checked");
}
@Test
@ExtendWith(ConvertException.class)
void testConvert() {
throw new RuntimeException("unchecked");
}
@Test
@ExtendWith(ShouldNotBeCalled.class)
@ExtendWith(SwallowException.class)
@ExtendWith(RethrowException.class)
@ExtendWith(ConvertException.class)
void testSeveral() {
throw new RuntimeException("unchecked");
}
}
static class RethrowException implements TestExecutionExceptionHandler {
static boolean handleExceptionCalled = false;
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
assertInstanceOf(IOException.class, throwable);
handleExceptionCalled = true;
handlerCalls.add("rethrow");
throw throwable;
}
}
static class SwallowException implements TestExecutionExceptionHandler {
static boolean handleExceptionCalled = false;
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) {
assertInstanceOf(IOException.class, throwable);
handleExceptionCalled = true;
handlerCalls.add("swallow");
//swallow exception by not rethrowing it
}
}
static class ConvertException implements TestExecutionExceptionHandler {
static boolean handleExceptionCalled = false;
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
assertInstanceOf(RuntimeException.class, throwable);
handleExceptionCalled = true;
handlerCalls.add("convert");
throw new IOException("checked");
}
}
static class ShouldNotBeCalled implements TestExecutionExceptionHandler {
static boolean handleExceptionCalled = false;
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) {
handleExceptionCalled = true;
}
}
}