JUnit: @Before only for some test methods?

前端 未结 6 1414
我寻月下人不归
我寻月下人不归 2020-12-30 18:29

I have some common set up code that I\'ve factored out to a method marked with @Before. However, it is not necessary for all this code to run for every single t

6条回答
  •  醉话见心
    2020-12-30 19:13

    It is possible to achieve also via Assume from JUnit. And then you can check the method name for which you want to process @Before.

    public class MyTest {
         @Rule
         public TestName testName = new TestName();
    
         @Before
         public void setUp() {
           assumeTrue(testName.getMethodName().equals("myMethodName"));
           // setup follows
         }
    }
    

    Check the topic for more insights about @Rule.

提交回复
热议问题