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

…衆ロ難τιáo~ 提交于 2019-12-18 06:19:27

问题


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 like @SkipOnFail that will set method as skipped if exception was thrown.

thanks in advance!

@Edit(for my question to be more clear)

@Test(expected=ConcreteException.class)

does not work for me because i need my tests to pass even if ConcreteException.class was not thrown(expected tag in junit will mark my test as failed if this exception won't be thrown), and to be skipped otherwise. In all other cases it should work as always.

@Solution that worked for me(junit v4.7) thx to @axtavt

@Rule
public MethodRule skipRule = new MethodRule() {
    public Statement apply(final Statement base, FrameworkMethod method, Object target) {
        if(method.getAnnotation(SkipOnFail.class) == null) return base;

        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try{
                    base.evaluate();
                } catch (ConcreteException e) {
                    Assume.assumeTrue(false);
                }
            }
        };
    }
};

@Thx


回答1:


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);
                 }
             }
         };
    }
};



回答2:


What about using JUnit Extensions?

The following example is taken from their Tutorial.

It provides aditional annotations for Prerequisites (@Prerequisite): Ignore tests based on conditions.

The required approach would be to check this during running tests. So you can simply add a @Prerequisite(requires="") annotation.

public class TestFillDatabase {
    @Prerequisite(requires = "databaseIsAvailable")
    @Test public void fillData() {
        // ...
    }

    public boolean databaseIsAvailable() {
        boolean isAvailable = ...;
        return isAvailable;
    }
}
public class TestFillDatabase {
    @Prerequisite(requires = "databaseIsAvailable")
    @Test public void fillData() {
        // ...
    }
    public boolean databaseIsAvailable() {
        boolean isAvailable = ...;
        return isAvailable ;
    }
}

This specified methods with @Prerequisite(requires = "databaseIsAvailable") must be a public method, returning a boolean or Boolean value.

If these methods will be consolidated in helper classes, you can also specify static methods within a class to be called using @Prerequisite(requires = "databaseIsAvailable", callee="DBHelper").

public class TestFillDatabase {
    @Prerequisite(requires = "databaseIsAvailable", callee="DBHelper")
    @Test public void fillData() {
        // ...
    }
}
public class DBHelper {
    public static boolean databaseIsAvailable() {
        boolean isAvailable = ...;
        return isAvailable ;
    }
}



回答3:


Also using the Assume class (since jUnit 4.4), you can use assumeNoException():

try{
    base.evaluate();
} catch (ConcreteException e) {
    Assume.assumeNoException("Concrete exception: skipping test", e);
}



回答4:


I searched for the docs about JUnit and it appears that from version 4.9 they have introduced what they call test rules (see TestRule). You may start from this.

The ExpectedException class marked as @Rule could be of some help in order to check for exceptions thrown but not mandatory for the test to pass.

For more advanced usage I cannot say for the moment as I've just discovered it.



来源:https://stackoverflow.com/questions/10804039/how-to-create-own-annotation-for-junit-that-will-skip-test-if-concrete-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!