Junit : how to make conditional tests?

社会主义新天地 提交于 2020-01-25 04:47:06

问题


I'm trying to write conditional tests with JUnit. I need to test the persistence of objects and if those tests pass, test access via HTTP methods (in order to develop a REST Web Service).

For this moment my solution looks like this :

public class ApplicationTest {

    @Test
    public void testSuite() {
        final Request requestStatutTest = Request.method(this.getClass(), "insertStatutTest");
        final Result resStatutTest = new JUnitCore().run(requestStatutTest);
        if (resStatutTest.wasSuccessful()) {
            postStatutTest();
            getStatutTest();
            putStatutTest();
            deleteStatutTest();

    }

    public void insertStatutTest() {
    }

    public void postStatutTest() {
    }

    // etc...

}

Is it the good solution ?


回答1:


Rather than assert that the property is true, you can use org.junit.Assume:

 @Before
 public void checkAssumptions() {
     org.junit.Assume.assumeTrue(someCondition());
     // or import static org.junit.Assume.* and then just call assumeTrue()
 }

If the condition is false, then this will result in the test ending with a violated assumption. As the docs say:

A failed assumption does not mean the code is broken, but that the test provides no useful information.

This is a weaker condition than a failed assertion (i.e. a test failure), and the default JUnit runner will treat this test as ignored. Which sounds like exactly what you are looking for - run the test if the persistence works, otherwise ignore it.



来源:https://stackoverflow.com/questions/19358444/junit-how-to-make-conditional-tests

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