Skip to content

Commit 5724d9c

Browse files
#5261 : refactored the test class
1 parent bb179a5 commit 5724d9c

File tree

1 file changed

+44
-58
lines changed

1 file changed

+44
-58
lines changed

platform-tests/src/test/java/org/junit/platform/launcher/core/LauncherPreconditionTests.java

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@
1010

1111
package org.junit.platform.launcher.core;
1212

13-
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertNull;
15+
import static org.junit.jupiter.api.Assertions.assertThrows;
1416
import static org.junit.platform.engine.support.store.NamespacedHierarchicalStore.CloseAction.closeAutoCloseables;
1517

1618
import java.util.List;
19+
import java.util.function.Supplier;
20+
import java.util.stream.Stream;
1721

18-
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.Arguments;
24+
import org.junit.jupiter.params.provider.MethodSource;
25+
import org.junit.platform.commons.PreconditionViolationException;
1926
import org.junit.platform.engine.support.store.Namespace;
2027
import org.junit.platform.engine.support.store.NamespacedHierarchicalStore;
2128
import org.junit.platform.fakes.TestEngineStub;
@@ -30,70 +37,49 @@
3037
@SuppressWarnings("NullAway")
3138
class LauncherPreconditionTests {
3239

33-
private final Launcher launcher = LauncherFactoryForTestingPurposesOnly.createLauncher(new TestEngineStub());
34-
35-
@Test
36-
@SuppressWarnings("DataFlowIssue")
37-
void sessionPerRequestLauncherRejectsNullDiscoveryRequest() {
38-
assertPreconditionViolationFor(() -> launcher.discover(null)) //
39-
.withMessageContaining("LauncherDiscoveryRequest must not be null");
40+
@ParameterizedTest(name = "{0}")
41+
@MethodSource("launcherSuppliers")
42+
void launcherRejectsNullRequests(String displayName, Supplier<Launcher> launcherSupplier) {
43+
assertRejectsNullRequests(launcherSupplier.get());
4044
}
4145

42-
@Test
43-
@SuppressWarnings("DataFlowIssue")
44-
void sessionPerRequestLauncherRejectsNullExecutionRequests() {
45-
assertPreconditionViolationFor(() -> launcher.execute((LauncherDiscoveryRequest) null)) //
46-
.withMessageContaining("LauncherDiscoveryRequest must not be null");
47-
assertPreconditionViolationFor(() -> launcher.execute((TestPlan) null)) //
48-
.withMessageContaining("TestPlan must not be null");
49-
assertPreconditionViolationFor(() -> launcher.execute((LauncherExecutionRequest) null)) //
50-
.withMessageContaining("LauncherExecutionRequest must not be null");
46+
private static Stream<Arguments> launcherSuppliers() {
47+
return Stream.of(
48+
Arguments.of("session-per-request launcher",
49+
(Supplier<Launcher>) () -> LauncherFactoryForTestingPurposesOnly
50+
.createLauncher(new TestEngineStub())),
51+
Arguments.of("default launcher",
52+
(Supplier<Launcher>) () -> new DefaultLauncher(List.of(new TestEngineStub()), List.of(),
53+
new NamespacedHierarchicalStore<Namespace>(null, closeAutoCloseables()))),
54+
Arguments.of("delegating launcher", (Supplier<Launcher>) () -> new DelegatingLauncher(new NoOpLauncher())),
55+
Arguments.of("intercepting launcher",
56+
(Supplier<Launcher>) () -> new InterceptingLauncher(new NoOpLauncher(),
57+
new NoOpLauncherInterceptor())));
5158
}
5259

53-
@Test
54-
@SuppressWarnings("DataFlowIssue")
55-
void defaultLauncherRejectsNullRequests() {
56-
var defaultLauncher = new DefaultLauncher(List.of(new TestEngineStub()), List.of(),
57-
new NamespacedHierarchicalStore<Namespace>(null, closeAutoCloseables()));
58-
59-
assertPreconditionViolationFor(() -> defaultLauncher.discover(null)) //
60-
.withMessageContaining("LauncherDiscoveryRequest must not be null");
61-
assertPreconditionViolationFor(() -> defaultLauncher.execute((LauncherDiscoveryRequest) null)) //
62-
.withMessageContaining("LauncherDiscoveryRequest must not be null");
63-
assertPreconditionViolationFor(() -> defaultLauncher.execute((TestPlan) null)) //
64-
.withMessageContaining("TestPlan must not be null");
65-
assertPreconditionViolationFor(() -> defaultLauncher.execute((LauncherExecutionRequest) null)) //
66-
.withMessageContaining("LauncherExecutionRequest must not be null");
60+
private static void assertRejectsNullRequests(Launcher launcher) {
61+
assertPreconditionViolationExactly(() -> launcher.discover(nullValue(LauncherDiscoveryRequest.class)),
62+
"LauncherDiscoveryRequest must not be null");
63+
assertPreconditionViolationExactly(() -> launcher.execute(nullValue(LauncherDiscoveryRequest.class)),
64+
"LauncherDiscoveryRequest must not be null");
65+
assertPreconditionViolationExactly(() -> launcher.execute(nullValue(TestPlan.class)), "TestPlan must not be null");
66+
assertPreconditionViolationExactly(() -> launcher.execute(nullValue(LauncherExecutionRequest.class)),
67+
"LauncherExecutionRequest must not be null");
6768
}
6869

69-
@Test
70-
@SuppressWarnings("DataFlowIssue")
71-
void delegatingLauncherRejectsNullRequests() {
72-
var delegatingLauncher = new DelegatingLauncher(new NoOpLauncher());
73-
74-
assertPreconditionViolationFor(() -> delegatingLauncher.discover(null)) //
75-
.withMessageContaining("LauncherDiscoveryRequest must not be null");
76-
assertPreconditionViolationFor(() -> delegatingLauncher.execute((LauncherDiscoveryRequest) null)) //
77-
.withMessageContaining("LauncherDiscoveryRequest must not be null");
78-
assertPreconditionViolationFor(() -> delegatingLauncher.execute((TestPlan) null)) //
79-
.withMessageContaining("TestPlan must not be null");
80-
assertPreconditionViolationFor(() -> delegatingLauncher.execute((LauncherExecutionRequest) null)) //
81-
.withMessageContaining("LauncherExecutionRequest must not be null");
70+
private static void assertPreconditionViolationExactly(Runnable action, String expectedMessage) {
71+
var ex = assertThrows(PreconditionViolationException.class, action::run);
72+
assertEquals(PreconditionViolationException.class, ex.getClass());
73+
assertEquals(expectedMessage, ex.getMessage());
74+
assertNull(ex.getCause());
8275
}
8376

84-
@Test
85-
@SuppressWarnings("DataFlowIssue")
86-
void interceptingLauncherRejectsNullRequests() {
87-
var interceptingLauncher = new InterceptingLauncher(new NoOpLauncher(), new NoOpLauncherInterceptor());
88-
89-
assertPreconditionViolationFor(() -> interceptingLauncher.discover(null)) //
90-
.withMessageContaining("LauncherDiscoveryRequest must not be null");
91-
assertPreconditionViolationFor(() -> interceptingLauncher.execute((LauncherDiscoveryRequest) null)) //
92-
.withMessageContaining("LauncherDiscoveryRequest must not be null");
93-
assertPreconditionViolationFor(() -> interceptingLauncher.execute((TestPlan) null)) //
94-
.withMessageContaining("TestPlan must not be null");
95-
assertPreconditionViolationFor(() -> interceptingLauncher.execute((LauncherExecutionRequest) null)) //
96-
.withMessageContaining("LauncherExecutionRequest must not be null");
77+
/**
78+
* Produces a typed {@code null} to avoid overload ambiguity and centralize nullability suppressions.
79+
*/
80+
@SuppressWarnings({ "DataFlowIssue", "NullAway", "unused" })
81+
private static <T> T nullValue(Class<T> type) {
82+
return null;
9783
}
9884

9985
private static final class NoOpLauncher implements Launcher {

0 commit comments

Comments
 (0)