PMDTestRunner should not fail, if there are no unit tests

This commit is contained in:
Andreas Dangel
2017-09-08 11:06:49 +02:00
parent 276028d04e
commit 9498473c20
2 changed files with 48 additions and 5 deletions

View File

@@ -4,6 +4,9 @@
package net.sourceforge.pmd.testframework;
import java.util.Collections;
import java.util.List;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
@@ -21,8 +24,8 @@ import org.junit.runners.model.InitializationError;
* and our custom {@link RuleTestRunner}.
* It allows to selectively execute single test cases (it is {@link Filterable}).
*
* <p>Note: Since we actually run two runner one after another, the static {@code BeforeClass}
* and {@Code AfterClass} methods will be executed twice.</p>
* <p>Note: Since we actually run two runners one after another, the static {@code BeforeClass}
* and {@Code AfterClass} methods will be executed twice and the test class will be instantiated twice, too.</p>
*
* <p>In order to use it, you'll need to subclass {@link SimpleAggregatorTst} and
* annotate your test class with RunWith:</p>
@@ -37,12 +40,17 @@ import org.junit.runners.model.InitializationError;
public class PMDTestRunner extends Runner implements Filterable, Sortable {
private final Class<? extends SimpleAggregatorTst> klass;
private final RuleTestRunner ruleTests;
private final JUnit4 unitTests;
private final ParentRunner<?> unitTests;
public PMDTestRunner(final Class<? extends SimpleAggregatorTst> klass) throws InitializationError {
this.klass = klass;
ruleTests = new RuleTestRunner(klass);
unitTests = new JUnit4(klass);
if (ruleTests.hasUnitTests()) {
unitTests = new JUnit4(klass);
} else {
unitTests = new EmptyRunner(klass);
}
}
@Override
@@ -74,7 +82,7 @@ public class PMDTestRunner extends Runner implements Filterable, Sortable {
return description;
}
private Description createChildrenDescriptions(ParentRunner<?> runner, String suiteName) {
private Description createChildrenDescriptions(Runner runner, String suiteName) {
Description suite = Description.createSuiteDescription(suiteName);
for (Description child : runner.getDescription().getChildren()) {
suite.addChild(child);
@@ -93,4 +101,30 @@ public class PMDTestRunner extends Runner implements Filterable, Sortable {
ruleTests.sort(sorter);
unitTests.sort(sorter);
}
private static class EmptyRunner extends ParentRunner<Object> {
protected EmptyRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
public Description getDescription() {
return Description.EMPTY;
}
@Override
protected List<Object> getChildren() {
return Collections.emptyList();
}
@Override
protected Description describeChild(Object child) {
return Description.EMPTY;
}
@Override
protected void runChild(Object child, RunNotifier notifier) {
// there are no tests - nothing to execute
}
}
}

View File

@@ -13,6 +13,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.rules.RunRules;
@@ -54,6 +55,14 @@ public class RuleTestRunner extends ParentRunner<TestDescriptor> {
return description;
};
/**
* Checks whether this test class has additionally unit test methods.
* @return true if there is at least one unit test method.
*/
public boolean hasUnitTests() {
return !getTestClass().getAnnotatedMethods(Test.class).isEmpty();
}
@Override
protected List<TestDescriptor> getChildren() {
List<Rule> rules = new ArrayList<>(instance.getRules());