Skip to content

Commit 651d949

Browse files
committed
Move long anonymous class to named class
1 parent bc34b7c commit 651d949

1 file changed

Lines changed: 62 additions & 37 deletions

File tree

src/pitest/java/net/kautler/test/pitest/SpockTestUnit.java

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,43 +67,7 @@ public void execute(ResultCollector resultCollector) {
6767
.selectors(DiscoverySelectors.selectUniqueId(testIdentifier.getUniqueId()))
6868
.build();
6969

70-
launcher.registerTestExecutionListeners(new TestExecutionListener() {
71-
@Override
72-
public void executionSkipped(TestIdentifier testIdentifier, String reason) {
73-
if (testIdentifier.isTest()) {
74-
resultCollector.notifySkipped(new Description(testIdentifier.getUniqueId(), testClass));
75-
}
76-
}
77-
78-
@Override
79-
public void executionStarted(TestIdentifier testIdentifier) {
80-
if (testIdentifier.isTest()) {
81-
resultCollector.notifyStart(new Description(testIdentifier.getUniqueId(), testClass));
82-
}
83-
}
84-
85-
@Override
86-
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
87-
Optional<Throwable> throwable = testExecutionResult.getThrowable();
88-
if (testIdentifier.isTest()) {
89-
if (TestExecutionResult.Status.ABORTED == testExecutionResult.getStatus()) {
90-
// abort treated as success
91-
// see: https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html
92-
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass));
93-
} else if (throwable.isPresent()) {
94-
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass), throwable.get());
95-
} else {
96-
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass));
97-
}
98-
} else {
99-
// Classes with failing BeforeAll methods identify as containers, not tests.
100-
if (throwable.isPresent()) {
101-
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass), throwable.get());
102-
}
103-
}
104-
}
105-
106-
});
70+
launcher.registerTestExecutionListeners(new ResultCollectorNotifier(resultCollector, testClass));
10771
launcher.execute(launcherDiscoveryRequest);
10872
}
10973

@@ -132,4 +96,65 @@ public String toString() {
13296
.add("displayName=" + testIdentifier.getDisplayName())
13397
.toString();
13498
}
99+
100+
/**
101+
* A test execution listener that notifies the given result collector about executed tests.
102+
*/
103+
private static class ResultCollectorNotifier implements TestExecutionListener {
104+
/**
105+
* The result collector to be notified about executed tests.
106+
*/
107+
private final ResultCollector resultCollector;
108+
109+
/**
110+
* The test class to be used for the created {@code Description}s.
111+
*/
112+
private final Class<?> testClass;
113+
114+
/**
115+
* Constructs a new Spock test unit.
116+
*
117+
* @param resultCollector the result collector to be notified about executed tests
118+
* @param testClass the test class to be used for the created {@code Description}s
119+
*/
120+
public ResultCollectorNotifier(ResultCollector resultCollector, Class<?> testClass) {
121+
this.resultCollector = resultCollector;
122+
this.testClass = testClass;
123+
}
124+
125+
@Override
126+
public void executionSkipped(TestIdentifier testIdentifier, String reason) {
127+
if (testIdentifier.isTest()) {
128+
resultCollector.notifySkipped(new Description(testIdentifier.getUniqueId(), testClass));
129+
}
130+
}
131+
132+
@Override
133+
public void executionStarted(TestIdentifier testIdentifier) {
134+
if (testIdentifier.isTest()) {
135+
resultCollector.notifyStart(new Description(testIdentifier.getUniqueId(), testClass));
136+
}
137+
}
138+
139+
@Override
140+
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
141+
Optional<Throwable> throwable = testExecutionResult.getThrowable();
142+
if (testIdentifier.isTest()) {
143+
if (TestExecutionResult.Status.ABORTED == testExecutionResult.getStatus()) {
144+
// abort treated as success
145+
// see: https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html
146+
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass));
147+
} else if (throwable.isPresent()) {
148+
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass), throwable.get());
149+
} else {
150+
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass));
151+
}
152+
} else {
153+
// Classes with failing BeforeAll methods identify as containers, not tests.
154+
if (throwable.isPresent()) {
155+
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass), throwable.get());
156+
}
157+
}
158+
}
159+
}
135160
}

0 commit comments

Comments
 (0)