com.googlecode.junittoolbox.InnerTestClassesSuite Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-toolbox Show documentation
Show all versions of junit-toolbox Show documentation
Useful classes for writing automated tests with JUnit
package com.googlecode.junittoolbox;
import org.junit.experimental.runners.Enclosed;
import org.junit.internal.runners.ErrorReportingRunner;
import org.junit.runner.Runner;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import java.util.ArrayList;
import java.util.List;
/**
* Runs all inner test classes of the class
* annotated with @RunWith(InnerTestClassesSuite.class)
.
* In contrast to the {@link Enclosed} runner provided by
* JUnit,
* it detects if an inner class is actually a test class
* and ignores all other inner classes.
* Example:
* @RunWith(InnerTestClassesSuite.class)
* public class LoginBeanTests {
*
* public static class UnitTests {
* @Test
* public void test1() { ... }
* }
*
* @Configuration
* public static class IntegrationTestsConfig { ... }
*
* @RunWith(SpringJUnit4ClassRunner.class)
* @ContextConfiguration(classes = IntegrationTestsConfig.class)
* public static class IntegrationTests {
* @Test
* public void test2() { ... }
* }
* }
*
*/
public class InnerTestClassesSuite extends Suite {
private static List getRunnersForInnerTestClasses(Class> klass, RunnerBuilder runnerBuilder) {
Class>[] innerClasses = klass.getClasses();
List runners = new ArrayList();
for (Class> innerClass : innerClasses) {
try {
Runner runner = runnerBuilder.runnerForClass(innerClass);
if (runner instanceof ErrorReportingRunner) {
// runnerBuilder.runnerForClass(innerClass) failed,
// inner class is not a test class and therefore ignored
} else {
runners.add(runner);
}
} catch (Throwable ignored) {
// runnerBuilder.runnerForClass(innerClass) failed,
// inner class is not a test class and therefore ignored
}
}
return runners;
}
public InnerTestClassesSuite(Class> klass, RunnerBuilder runnerBuilder) throws InitializationError {
super(klass, getRunnersForInnerTestClasses(klass, runnerBuilder));
}
}