Skip to content

Commit b06e052

Browse files
authored
Comprehensively test ExecutionListenerAdapter (#5202)
With mockito/mockito#146 resolved it is now possible to spy on the TestExecutionListener which enables testing the ExecutionListenerAdapter without undue verbosity.
1 parent e8bc3fc commit b06e052

File tree

1 file changed

+84
-36
lines changed

1 file changed

+84
-36
lines changed

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

Lines changed: 84 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,122 @@
1010

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

13-
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.junit.platform.launcher.core.OutputDirectoryCreators.dummyOutputDirectoryCreator;
15-
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.verify;
14+
import static org.mockito.Mockito.when;
15+
import static org.mockito.quality.Strictness.STRICT_STUBS;
1616

17-
import java.util.Map;
17+
import java.nio.file.Path;
1818

19-
import org.jspecify.annotations.Nullable;
19+
import org.junit.jupiter.api.AfterEach;
2020
import org.junit.jupiter.api.Test;
2121
import org.junit.platform.commons.util.ReflectionUtils;
2222
import org.junit.platform.engine.TestDescriptor;
23+
import org.junit.platform.engine.TestExecutionResult;
2324
import org.junit.platform.engine.UniqueId;
25+
import org.junit.platform.engine.reporting.FileEntry;
2426
import org.junit.platform.engine.reporting.ReportEntry;
2527
import org.junit.platform.engine.support.descriptor.DemoMethodTestDescriptor;
2628
import org.junit.platform.launcher.TestExecutionListener;
2729
import org.junit.platform.launcher.TestIdentifier;
28-
import org.junit.platform.launcher.core.LauncherDiscoveryResult.EngineResultInfo;
30+
import org.junit.platform.launcher.TestPlan;
31+
import org.mockito.Mock;
32+
import org.mockito.Mockito;
33+
import org.mockito.junit.jupiter.MockitoSettings;
2934

3035
/**
3136
* @since 1.0
3237
*/
33-
// TODO Test other adapter methods.
38+
@MockitoSettings(strictness = STRICT_STUBS)
3439
class ExecutionListenerAdapterTests {
3540

41+
final UniqueId uniqueId = UniqueId.root("method", "demoTestMethod");
42+
final TestDescriptor testDescriptor = createDemoMethodTestDescriptor();
43+
final TestIdentifier testIdentifier = TestIdentifier.from(testDescriptor);
44+
45+
@Mock
46+
TestPlan testPlan;
47+
48+
@Mock
49+
TestExecutionListener testExecutionListener;
50+
51+
@AfterEach
52+
void verifyNoMoreInteractions() {
53+
Mockito.verifyNoMoreInteractions(testPlan, testExecutionListener);
54+
}
55+
3656
@Test
37-
void testReportingEntryPublished() {
38-
var testDescriptor = getSampleMethodTestDescriptor();
57+
void dynamicTestRegistered() {
58+
var executionListenerAdapter = new ExecutionListenerAdapter(testPlan, testExecutionListener);
3959

40-
var discoveryResult = new LauncherDiscoveryResult(
41-
Map.of(mock(), EngineResultInfo.completed(testDescriptor, DiscoveryIssueNotifier.NO_ISSUES)), mock(),
42-
dummyOutputDirectoryCreator());
43-
var testPlan = InternalTestPlan.from(discoveryResult);
44-
var testIdentifier = testPlan.getTestIdentifier(testDescriptor.getUniqueId());
60+
executionListenerAdapter.dynamicTestRegistered(testDescriptor);
61+
62+
verify(testExecutionListener).dynamicTestRegistered(testIdentifier);
63+
verify(testPlan).addInternal(testIdentifier);
64+
}
4565

46-
//not yet spyable with mockito? -> https://github.com/mockito/mockito/issues/146
47-
var testExecutionListener = new MockTestExecutionListener();
66+
@Test
67+
void executionStarted() {
4868
var executionListenerAdapter = new ExecutionListenerAdapter(testPlan, testExecutionListener);
4969

50-
var entry = ReportEntry.from("one", "two");
51-
executionListenerAdapter.reportingEntryPublished(testDescriptor, entry);
70+
when(testPlan.getTestIdentifier(uniqueId)).thenReturn(testIdentifier);
71+
executionListenerAdapter.executionStarted(testDescriptor);
5272

53-
assertThat(testExecutionListener.entry).isEqualTo(entry);
54-
assertThat(testExecutionListener.testIdentifier).isEqualTo(testIdentifier);
73+
verify(testExecutionListener).executionStarted(testIdentifier);
5574
}
5675

57-
private TestDescriptor getSampleMethodTestDescriptor() {
58-
var localMethodNamedNothing = ReflectionUtils.findMethod(this.getClass(), "nothing",
59-
new Class<?>[0]).orElseThrow();
60-
return new DemoMethodTestDescriptor(UniqueId.root("method", "unique_id"), localMethodNamedNothing);
76+
@Test
77+
void executionSkipped() {
78+
var executionListenerAdapter = new ExecutionListenerAdapter(testPlan, testExecutionListener);
79+
var reason = "skip reason";
80+
81+
when(testPlan.getTestIdentifier(uniqueId)).thenReturn(testIdentifier);
82+
executionListenerAdapter.executionSkipped(testDescriptor, reason);
83+
84+
verify(testExecutionListener).executionSkipped(testIdentifier, reason);
6185
}
6286

63-
//for reflection purposes only
64-
void nothing() {
87+
@Test
88+
void executionFinished() {
89+
var executionListenerAdapter = new ExecutionListenerAdapter(testPlan, testExecutionListener);
90+
var testExecutionResult = TestExecutionResult.successful();
91+
92+
when(testPlan.getTestIdentifier(uniqueId)).thenReturn(testIdentifier);
93+
executionListenerAdapter.executionFinished(testDescriptor, testExecutionResult);
94+
95+
verify(testExecutionListener).executionFinished(testIdentifier, testExecutionResult);
6596
}
6697

67-
static class MockTestExecutionListener implements TestExecutionListener {
98+
@Test
99+
void testReportingEntryPublished() {
100+
var executionListenerAdapter = new ExecutionListenerAdapter(testPlan, testExecutionListener);
101+
var entry = ReportEntry.from("one", "two");
68102

69-
@Nullable
70-
public TestIdentifier testIdentifier;
103+
when(testPlan.getTestIdentifier(uniqueId)).thenReturn(testIdentifier);
104+
executionListenerAdapter.reportingEntryPublished(testDescriptor, entry);
71105

72-
@Nullable
73-
public ReportEntry entry;
106+
verify(testExecutionListener).reportingEntryPublished(testIdentifier, entry);
107+
}
74108

75-
@Override
76-
public void reportingEntryPublished(TestIdentifier testIdentifier, ReportEntry entry) {
77-
this.testIdentifier = testIdentifier;
78-
this.entry = entry;
79-
}
109+
@Test
110+
void fileEntryPublished() {
111+
var executionListenerAdapter = new ExecutionListenerAdapter(testPlan, testExecutionListener);
112+
var entry = FileEntry.from(Path.of("entry.txt"), "application/txt");
113+
114+
when(testPlan.getTestIdentifier(uniqueId)).thenReturn(testIdentifier);
115+
executionListenerAdapter.fileEntryPublished(testDescriptor, entry);
116+
117+
verify(testExecutionListener).fileEntryPublished(testIdentifier, entry);
118+
}
119+
120+
private TestDescriptor createDemoMethodTestDescriptor() {
121+
var demoTestMethod = ReflectionUtils.findMethod(ExecutionListenerAdapterTests.class,
122+
"demoTestMethod").orElseThrow();
123+
return new DemoMethodTestDescriptor(uniqueId, demoTestMethod);
124+
}
80125

126+
//for reflection purposes only
127+
@SuppressWarnings("unused")
128+
void demoTestMethod() {
81129
}
82130

83131
}

0 commit comments

Comments
 (0)