public abstract class Runner implements Describable {
public abstract Description getDescription();
public abstract void run(RunNotifier notifier);
public int testCount() {
return getDescription().testCount();
}
}
private final Object[] parameters;
private final String name;
public BlockJUnit4ClassRunnerWithParameters(TestWithParameters test)
throws InitializationError {
super(test.getTestClass().getJavaClass());
parameters = test.getParameters().toArray(
new Object[test.getParameters().size()]);
name = test.getName();
}
参数代码如下:
public class TestWithParameters {
private final String name;
private final TestClass testClass;
private final List
@RunWith(Suite.class)
@Suite.SuiteClasses({Suite_test_a.class,Suite_test_b.class,Suite_test_c.class })
public class Suite_main {
}
public class Suite_test_a {
@Test
public void testRun(){
System.out.println("Suite_test_a_running");
}
}
public class Suite_test_b {
@Test
public void testRun(){
System.out.println("Suite_test_b_running");
}
}
public class Suite_test_c {
@Test
public void testRun(){
System.out.println("Suite_test_c_running");
}
}
public interface BlackCategory {}
public interface WhiteCategory {}
public class Categories_test_a {
@Test
@Category(BlackCategory.class)
public void testFirst(){
System.out.println("Categories_test_a_testFirst_running");
}
@Test
@Category(WhiteCategory.class)
public void testSecond(){
System.out.println("Categories_test_a_testSecond_running");
}
}
public class Categories_test_b {
@Test
@Category(WhiteCategory.class)
public void testFirst(){
System.out.println("Categories_test_b_testFirst_running");
}
@Test
@Category(BlackCategory.class)
public void testSecond(){
System.out.println("Categories_test_b_testSecond_running");
}
}
执行带WhiteCategory的方法
@RunWith(Categories.class)
@Categories.IncludeCategory(WhiteCategory.class)
@Categories.ExcludeCategory(BlackCategory.class)
@Suite.SuiteClasses( { Categories_test_a.class, Categories_test_b.class })
public class Categories_main {
}
运行结果:
图4 Categories测试代码WhiteCategory分组执行结果
执行带BlackCategory的方法
@RunWith(Categories.class)
@Categories.IncludeCategory(BlackCategory.class)
@Categories.ExcludeCategory(WhiteCategory.class)
@Suite.SuiteClasses( { Categories_test_a.class, Categories_test_b.class })
public class Categories_main {
}
public class EnclosedTest {
@Test
public void runOutMethou(){
System.out.println("EnclosedTest_runOutMethou_running");
}
public static class EnclosedInnerTest {
@Test
public void runInMethou(){
System.out.println("EnclosedInnerTest_runInMethou_running");
}
}
}
运行结果:没有执行内部类的测试方法。
图6 Enclosed测试代码的执行结果
使用Enclosed执行器:
@RunWith(Enclosed.class)
public class EnclosedTest {
@Test
public void runOutMethou(){
System.out.println("EnclosedTest_runOutMethou_running");
}
public static class EnclosedInnerTest {
@Test
public void runInMethou(){
System.out.println("EnclosedInnerTest_runInMethou_running");
}
}
}
private final List causes;
public ErrorReportingRunner(Class> testClass, Throwable cause) {
if (testClass == null) {
throw new NullPointerException("Test class cannot be null");
}
this.testClass = testClass;
causes = getCauses(cause);
}
private List getCauses(Throwable cause) {
if (cause instanceof InvocationTargetException) {
return getCauses(cause.getCause());
}
if (cause instanceof InitializationError) {
return ((InitializationError) cause).getCauses();
}
if (cause instanceof org.junit.internal.runners.InitializationError) {
return ((org.junit.internal.runners.InitializationError) cause)
.getCauses();
}
return Arrays.asList(cause);
}
当junit运行错误时,会抛出ErrorReportingRunner,例如:
public Runner getRunner() {
try {
Runner runner = request.getRunner();
fFilter.apply(runner);
return runner;
} catch (NoTestsRemainException e) {
return new ErrorReportingRunner(Filter.class, new Exception(String
.format("No tests found matching %s from %s", fFilter
.describe(), request.toString())));
}
}
3.4 IgnoredClassRunner
IgnoredClassRunner是当测试的方法包含Ignore注解时,会忽略该方法。
public class IgnoredClassRunner extends Runner {
private final Class> clazz;
public IgnoredClassRunner(Class> testClass) {
clazz = testClass;
}
@Override
public void run(RunNotifier notifier) {
notifier.fireTestIgnored(getDescription());
}
@Override
public Description getDescription() {
return Description.createSuiteDescription(clazz);
}
}
IgnoredClassRunner的使用
public class IgnoredBuilder extends RunnerBuilder {
@Override
public Runner runnerForClass(Class> testClass) {
if (testClass.getAnnotation(Ignore.class) != null) {
return new IgnoredClassRunner(testClass);
}
return null;
}
}