Java 自定义Junit runner,在运行之前和之后
在unit custom runner中,我希望在运行测试操作之前和之后执行操作, 所以我想出了这个解决方案 这样做有多可靠,有没有更干净的方法Java 自定义Junit runner,在运行之前和之后,java,junit,junit4,junit-runner,Java,Junit,Junit4,Junit Runner,在unit custom runner中,我希望在运行测试操作之前和之后执行操作, 所以我想出了这个解决方案 这样做有多可靠,有没有更干净的方法 public class SomeCustomRunner extends BlockJUnit4ClassRunner { private int m_testMethodIndex = 0; private int m_testMethodsCount = 0; private boolean m_sessionSet
public class SomeCustomRunner extends BlockJUnit4ClassRunner {
private int m_testMethodIndex = 0;
private int m_testMethodsCount = 0;
private boolean m_sessionSetup = false;
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
if(m_sessionSetup == false) {
m_sessionSetup = true;
beforeTestClass(); //->> DO MY STUFF HERE
}
super.runChild(method, notifier);
m_testMethodIndex++;
if(m_testMethodIndex == m_testMethodsCount) {
afterTestClass(); //->> DO MY STUFF HERE
}
}
@Override
protected List<FrameworkMethod> getChildren() {
List<FrameworkMethod> methods = super.getChildren();
m_testMethodsCount = methods.size();
return methods;
}
}
public类SomeCustomRunner扩展了BlockJUnit4ClassRunner{
私有int m_testMethodIndex=0;
私有int m_testMethodsCount=0;
私有布尔m_sessionSetup=false;
@凌驾
受保护的void runChild(最终框架方法,RunNotifier通知程序){
if(m_sessionSetup==false){
m_sessionSetup=true;
beforeTestClass();//->在这里完成我的工作
}
super.runChild(方法、通知程序);
m_testMethodIndex++;
if(m_testMethodIndex==m_testmethodscont){
PostTestClass();//->在这里完成我的工作
}
}
@凌驾
受保护列表getChildren(){
List methods=super.getChildren();
m_testMethodsCount=methods.size();
返回方法;
}
}
您可以在用@BeforeClass
resp注释的方法中定义在测试类本身之前和之后执行的操作,而不是创建单独的测试运行程序<代码>@AfterClass
要在多个测试中重用它们,您可以很容易地从基类继承它们。最简单的方法是重写
run
方法,如下所示:
public class LifecycleRunner extends BlockJUnit4ClassRunner {
public LifecycleRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
public void run(RunNotifier notifier) {
beforeRunTests();
try {
super.run(notifier);
} finally {
afterRunTests();
}
}
private void afterRunTests() {
trace();
}
private void beforeRunTests() {
trace();
}
private void trace() {
System.out.println(Thread.currentThread().getStackTrace()[2]);
}
}
公共类LifecycleRunner扩展了BlockJUnit4ClassRunner{
public LifecycleRunner(类klass)引发初始化错误{
超级(klass);;
}
@凌驾
公共无效运行(运行通知程序通知程序){
在运行测试()之前;
试一试{
super.run(通知程序);
}最后{
后运行测试();
}
}
私有void afterRunTests(){
trace();
}
运行测试之前的私有无效(){
trace();
}
私有无效跟踪(){
System.out.println(Thread.currentThread().getStackTrace()[2]);
}
}
要完全控制测试执行,请在运行程序中安装适当的RunListener
@Override
public void run(final RunNotifier notifier)
{
final RunListener listener = new RunListener()
{
@Override
public void testStarted(final Description description) throws Exception
{
// do something before each (non-ignored) Test method is executed
}
@Override
public void testFinished(final Description description) throws Exception
{
// do something after each Test method was performed (failed or successful)
}
// check further listener methods yourself!
};
// install your listener
notifier.addListener(listener);
super.run(notifier);
// and remove it again
notifier.removeListener(listener);
}
我知道这些,但我的跑垒员所做的远不止测试方法。因此,对于这种复杂的情况,我可能需要它。JUnitAPI似乎不支持这种情况。但我不确定您的解决方案在测试用例中出现异常、多线程等情况下的表现。所以我猜这个“黑客”是我必须找出的,也许有人已经尝试过了。