All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.junittoolbox.InnerTestClassesSuite Maven / Gradle / Ivy

There is a newer version: 2.4
Show newest version
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)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy