Skip to content

Commit 91b5b4a

Browse files
authored
Merge pull request #1401 from hcoles/feature/junt4_error_loggins
extract errors when JUnit4 classes cannot be loaded
2 parents eccd89c + 0c22a54 commit 91b5b4a

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.pitest.junit;
2+
3+
import org.junit.runner.notification.Failure;
4+
import org.junit.runner.notification.RunListener;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Optional;
9+
10+
/**
11+
* Extracts failure strings from junit error runners
12+
* Currently only for initialisation errors
13+
*/
14+
public class DebugListener extends RunListener {
15+
private final List<String> problems = new ArrayList<>();
16+
17+
@Override
18+
public void testFailure(Failure failure) {
19+
String exception = failure.getException().toString();
20+
if (exception.contains("NoClassDefFoundError") || exception.contains("ClassNotFoundException")) {
21+
problems.add(exception + "\n" + failure.getTrace());
22+
}
23+
24+
}
25+
26+
public Optional<String> problems() {
27+
if (problems.isEmpty()) {
28+
return Optional.empty();
29+
}
30+
return Optional.of(String.join("\n",problems));
31+
}
32+
33+
}

pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.junit.runner.Runner;
3737
import org.junit.runner.manipulation.Filter;
3838
import org.junit.runner.manipulation.Filterable;
39+
import org.junit.runner.notification.RunNotifier;
3940
import org.junit.runners.Parameterized;
4041
import org.pitest.functional.FCollection;
4142
import org.pitest.junit.adapter.AdaptedJUnitTestUnit;
@@ -46,6 +47,7 @@
4647
import org.pitest.testapi.TestUnitExecutionListener;
4748
import org.pitest.testapi.TestUnitFinder;
4849
import org.pitest.util.IsolationUtils;
50+
import org.pitest.util.Log;
4951

5052
public class JUnitCustomRunnerTestUnitFinder implements TestUnitFinder {
5153

@@ -71,6 +73,9 @@ public List<TestUnit> findTestUnits(final Class<?> clazz, TestUnitExecutionListe
7173
final Runner runner = AdaptedJUnitTestUnit.createRunner(clazz);
7274

7375
if (isExcluded(runner) || isNotARunnableTest(runner, clazz.getName()) || !isIncluded(clazz)) {
76+
if (Log.verbosity().showMinionOutput() && runner instanceof ErrorReportingRunner) {
77+
showJUnitErrors(clazz, runner);
78+
}
7479
return Collections.emptyList();
7580
}
7681

@@ -84,6 +89,14 @@ public List<TestUnit> findTestUnits(final Class<?> clazz, TestUnitExecutionListe
8489
}
8590
}
8691

92+
private void showJUnitErrors(Class<?> clazz, Runner runner) {
93+
RunNotifier notifier = new RunNotifier();
94+
DebugListener debugListener = new DebugListener();
95+
notifier.addListener(debugListener);
96+
runner.run(notifier);
97+
Log.getLogger().fine("Cannot find tests in " + clazz + " : " + debugListener.problems());
98+
}
99+
87100
private List<TestUnit> filterUnitsByMethod(List<TestUnit> filteredUnits) {
88101
if (this.includedTestMethods.isEmpty()) {
89102
return filteredUnits;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.pitest.junit;
2+
3+
import org.junit.Test;
4+
5+
import org.junit.runner.notification.Failure;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
public class DebugListenerTest {
10+
DebugListener testee = new DebugListener();
11+
12+
@Test
13+
public void reportsErrorsWhenNoClassDefFound() {
14+
testee.testFailure(new Failure(null, new Exception("NoClassDefFoundError")));
15+
assertThat(testee.problems()).isPresent();
16+
assertThat(testee.problems().get()).contains("NoClassDefFoundError");
17+
assertThat(testee.problems().get()).contains("at org.pitest.junit.DebugListenerTest");
18+
}
19+
20+
@Test
21+
public void reportsErrorsWhenClassNotFound() {
22+
testee.testFailure(new Failure(null, new Exception("ClassNotFoundException")));
23+
assertThat(testee.problems()).isPresent();
24+
assertThat(testee.problems().get()).contains("ClassNotFoundException");
25+
assertThat(testee.problems().get()).contains("at org.pitest.junit.DebugListenerTest");
26+
}
27+
28+
@Test
29+
public void doesNotReportErrorsForOtherExceptions() {
30+
testee.testFailure(new Failure(null, new Exception("foo bar")));
31+
assertThat(testee.problems()).isEmpty();
32+
}
33+
34+
}

0 commit comments

Comments
 (0)