Exclude individual test from 'before' method in JUnit

前端 未结 6 2028
耶瑟儿~
耶瑟儿~ 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 00:58

    Unfortunately you have to code this logic. JUnit does not have such feature. Generally you have 2 solutions:

    1. Just separate test case to 2 test cases: one that contains tests that require "before" running and second that contains tests that do not require this.
    2. Implement your own test running and annotate your test to use it. Create your own annotation @RequiresBefore and mark tests that need this with this annotation. The test runner will parse the annotation and decide whether to run "before" method or not.

    The second solution is clearer. The first is simpler. This is up to you to chose one of them.

提交回复
热议问题