How to run test methods in specific order in JUnit4?

后端 未结 18 1848
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 04:35

I want to execute test methods which are annotated by @Test in specific order.

For example:

public class MyTest {
    @Test public void          


        
18条回答
  •  礼貌的吻别
    2020-11-22 05:12

    Migration to TestNG seems the best way, but I see no clear solution here for jUnit. Here is most readable solution / formatting I found for jUnit:

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class SampleTest {
        @Test
        void stage1_prepareAndTest(){};
    
        @Test
        void stage2_checkSomething(){};
    
        @Test
        void stage2_checkSomethingElse(){};
    
        @Test
        void stage3_thisDependsOnStage2(){};
    
        @Test
        void callTimeDoesntMatter(){}
    }
    

    This ensures stage2 methods are called after stage1 ones and before stage3 ones.

提交回复
热议问题