|
10 | 10 |
|
11 | 11 | package org.junit.platform.launcher.core; |
12 | 12 |
|
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; |
16 | 16 |
|
17 | | -import java.util.Map; |
| 17 | +import java.nio.file.Path; |
18 | 18 |
|
19 | | -import org.jspecify.annotations.Nullable; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
20 | 20 | import org.junit.jupiter.api.Test; |
21 | 21 | import org.junit.platform.commons.util.ReflectionUtils; |
22 | 22 | import org.junit.platform.engine.TestDescriptor; |
| 23 | +import org.junit.platform.engine.TestExecutionResult; |
23 | 24 | import org.junit.platform.engine.UniqueId; |
| 25 | +import org.junit.platform.engine.reporting.FileEntry; |
24 | 26 | import org.junit.platform.engine.reporting.ReportEntry; |
25 | 27 | import org.junit.platform.engine.support.descriptor.DemoMethodTestDescriptor; |
26 | 28 | import org.junit.platform.launcher.TestExecutionListener; |
27 | 29 | 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; |
29 | 34 |
|
30 | 35 | /** |
31 | 36 | * @since 1.0 |
32 | 37 | */ |
33 | | -// TODO Test other adapter methods. |
| 38 | +@MockitoSettings(strictness = STRICT_STUBS) |
34 | 39 | class ExecutionListenerAdapterTests { |
35 | 40 |
|
| 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 | + |
36 | 56 | @Test |
37 | | - void testReportingEntryPublished() { |
38 | | - var testDescriptor = getSampleMethodTestDescriptor(); |
| 57 | + void dynamicTestRegistered() { |
| 58 | + var executionListenerAdapter = new ExecutionListenerAdapter(testPlan, testExecutionListener); |
39 | 59 |
|
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 | + } |
45 | 65 |
|
46 | | - //not yet spyable with mockito? -> https://github.com/mockito/mockito/issues/146 |
47 | | - var testExecutionListener = new MockTestExecutionListener(); |
| 66 | + @Test |
| 67 | + void executionStarted() { |
48 | 68 | var executionListenerAdapter = new ExecutionListenerAdapter(testPlan, testExecutionListener); |
49 | 69 |
|
50 | | - var entry = ReportEntry.from("one", "two"); |
51 | | - executionListenerAdapter.reportingEntryPublished(testDescriptor, entry); |
| 70 | + when(testPlan.getTestIdentifier(uniqueId)).thenReturn(testIdentifier); |
| 71 | + executionListenerAdapter.executionStarted(testDescriptor); |
52 | 72 |
|
53 | | - assertThat(testExecutionListener.entry).isEqualTo(entry); |
54 | | - assertThat(testExecutionListener.testIdentifier).isEqualTo(testIdentifier); |
| 73 | + verify(testExecutionListener).executionStarted(testIdentifier); |
55 | 74 | } |
56 | 75 |
|
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); |
61 | 85 | } |
62 | 86 |
|
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); |
65 | 96 | } |
66 | 97 |
|
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"); |
68 | 102 |
|
69 | | - @Nullable |
70 | | - public TestIdentifier testIdentifier; |
| 103 | + when(testPlan.getTestIdentifier(uniqueId)).thenReturn(testIdentifier); |
| 104 | + executionListenerAdapter.reportingEntryPublished(testDescriptor, entry); |
71 | 105 |
|
72 | | - @Nullable |
73 | | - public ReportEntry entry; |
| 106 | + verify(testExecutionListener).reportingEntryPublished(testIdentifier, entry); |
| 107 | + } |
74 | 108 |
|
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 | + } |
80 | 125 |
|
| 126 | + //for reflection purposes only |
| 127 | + @SuppressWarnings("unused") |
| 128 | + void demoTestMethod() { |
81 | 129 | } |
82 | 130 |
|
83 | 131 | } |
0 commit comments