Exclude individual test from 'before' method in JUnit

前端 未结 6 2017
耶瑟儿~
耶瑟儿~ 2021-01-04 00:16

All tests in my test class execute a \'before\' method (annotated with JUnit\'s @Before) before the execution of each test.

I need a particular test not

6条回答
  •  长发绾君心
    2021-01-04 01:16

    One can also solve this by undoing what was done in @Before setup inside test case. This is how it may look,

    @Before
    public void setup() {
        TestDataSetupClass.setupTestData();
    }
    
    @Test
    public void testServiceWithIgnoreCommonSetup() {
        TestDataSetupClass.unSet();
        //Perform Test
    }
    

    There will be pros and cons for solutions here. Minor con for this is, unnecessary cycle of setting and un-setting step. But goes well if one needs to do it for only a test case out of hundreds and avoid overhead of writing self AOP or maintaining multiple inner test classes.

提交回复
热议问题