How to create own annotation for junit that will skip test if concrete exception was thrown during execution?

后端 未结 4 1412
逝去的感伤
逝去的感伤 2020-12-19 11:20

My application have several execution modes, and in 1 mode it is normal that some of my tests will throw a concrete exception. I need to annotate this methods with something

4条回答
  •  失恋的感觉
    2020-12-19 11:42

    I don't think that such a feature is available out of the box, but it should be pretty easy to implement with custom TestRule and Assume, something like this:

    @Rule
    public TestRule skipRule = new TestRule() {
        public Statement apply(final Statement base, Description desc) {
             if (desc.getAnnotation(SkipOnFail.class) == null) return base;
    
             return new Statement() {
                 public void evaluate() throws Throwable {
                     try {
                         base.evaluate();
                     } catch (MyExceptoion ex) {
                         Assume.assumeTrue(false);
                     }
                 }
             };
        }
    };
    

提交回复
热议问题