2017-07-18 87 views
0

我目前使用Spring TestExecutionListener来处理我的测试类中的自定义注释。根据一些外部情况,我想忽略一个测试。让TestExecutionListener跳过/忽略测试

目前我使用beforeTestClassbeforeTestMethod中的某个假设来实现此功能。但不幸的是,它在日志中创建了一个警告,包括一个远离嘈杂的堆栈跟踪。

是否有可能以编程方式忽略测试(可能使用TestContext)?

+0

难道你不能仅仅为测试添加'@ActiveProfiles(profiles = {“dev”}'并关闭配置文件吗? – StanislavL

+0

由于复杂的硬件需求,如果没有复杂化就不可能实现,因此我们创建了自己的注解根据所连接的设备跳过测试 – Nitek

回答

2

我想你可以尝试扩展默认的测试运行,并提供isIgnored方法的自定义实现:

public class CustomRunner extends BlockJUnit4ClassRunner { 
    public CustomRunner(Class<?> c) throws initializationError { 
     super(c); 
    } 

    @Override 
    protected boolean isIgnored(FrameworkMethod child) { 
     if(shouldIgnore()) { 
      return true; 
     } 
     return super.isIgnored(child); 
    } 

    private boolean shouldIgnore() { 
     /* some custom criteria */ 
    } 
} 

这里,base implementation忽略具有@Ignore注释测试,但您可以提供自己的实现的忽略标准。