abstract class ParentRunner<T> extends Runner implements Filterable,Sortable
本节介绍排序和过滤。(虽然JUnit4.8.2源代码分析-6.1 排序和过滤中演示了客户使用排序和过滤的方式,也有些不明白其设计意图,但是,先读懂源代码为妙。说不定看着看着就明白了。)
排序和过滤的相关类型,在org.junit.runner.manipulation包中。
1.例如Sortable是一个函数接口,yqj2065觉得Sortable.sort(Sorter)不如叫Sortable.setSorter(Sorter)。
package org.junit.runner.manipulation;
public interface Sortable {
public void sort(Sorter sorter);
}2.Filterable类似,定义了void filter(Filter filter) throws NoTestsRemainException;
3.NoTestsRemainException抱怨filter 将所有的测试都过滤掉了。
4.Sorter implements Comparator<Description>。我们知道,java.util.Comparator接口是一个策略类,定义了int compare(T o1, T o2)方法。而Sorter是一个简单的代理模式的Proxy角色。通过构造器注入的方式Sorter(Comparator<Description>comparator)初始化realSubject角色,测试程序员可以定义自己的排序器。而且,Sorter应用Null
Object模式,定义了NULL静态内部子类。
package org.junit.runner.manipulation;
import java.util.Comparator;
import org.junit.runner.Description;
public class Sorter implements Comparator<Description> {
/**
* NULL is a <code>Sorter</code> that leaves elements in an undefined order
*/
public static Sorter NULL= new Sorter(new Comparator<Description>() {
public int compare(Description o1, Description o2) {
return 0;
}});
public void apply(Object object) {
if (object instanceof Sortable) {
Sortable sortable = (Sortable) object;
sortable.sort(this);
}
}
}
Sorter的方法apply(Object object),将this应用于Object 身上。
5.Filter是一个抽象类,当需要过滤某些测试时,测试程序员要定义自己的过滤器。Filter应用Null Object模式,定义了ALL静态内部子类。静态工厂生成public static Filter matchMethodDescription(final Description desiredDescription)。而public Filter intersect(final Filter second)允许多个Filter串接。
按照上述几个例子,我们可以写出自己的过滤器。
package sortFilter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.junit.runner.Description;
import org.junit.runner.manipulation.Filter;
/**
*排除指定的方法。
* @author yqj2065
*/
public class MethodNameFilter extends Filter {
private final Set<String> excludedMethods = new HashSet<>();
public MethodNameFilter(String... excludedMethods) {
this.excludedMethods.addAll(Arrays.asList(excludedMethods));
}
@Override
public boolean shouldRun(Description description) {
String methodName = description.getMethodName();
return !excludedMethods.contains(methodName);
}
@Override
public String describe() {
return this.getClass().getSimpleName() + "-excluded methods: "
+ excludedMethods;
}
}Unit4有测试方法a() 、@Ignore b() 、c() 和d()等,
package demo;
import static tool.Print.*;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import sortFilter.MethodNameFilter;
import units.Unit4;
/**
*
* @author yqj2065
*/
public class FilterDemo {
public static void main(String... args) {
try {
BlockJUnit4ClassRunner runner = null;
try {
runner = new BlockJUnit4ClassRunner(Unit4.class);
} catch (InitializationError e) {
}
runner.filter(new MethodNameFilter("a","b","c", "d"));//排除所有
runner.run(new RunNotifier());
} catch (NoTestsRemainException ex) {
pln(ex);
}
}
}输出:org.junit.runner.manipulation.NoTestsRemainException
保留@Ignore b(),虽然没有执行测试,但是不抛出异常。
原文地址:http://blog.csdn.net/yqj2065/article/details/40373225