How does assumeTrue() work in JUnit

后端 未结 1 1176
生来不讨喜
生来不讨喜 2021-02-19 07:00

I\'m looking for some validation as to how Assume.assumeTrue() works in JUnit. I\'d like to use it in a @BeforeClass method so that I can avoid running

相关标签:
1条回答
  • 2021-02-19 07:31

    @Before, @BeforeClass annotated methods are setUp methods i.e methods you use to configure mock behaviour that will be used by your test methods. This is not used for running any test cases. Same is the case with @After and @AfterClass, it should be used to annotate tear down methods and should not contain any tests.

    Now, you should use Assume.assumeTrue() in your test methods(@Test annotation). Yes, assumeTrue(), if called with an expression evaluating to false, the test will halt and be ignored. Check docs for more info.

    P.S - This might not be the answer, but it is to help her go in the right direction.

    EDIT -

        To me it was a little surprising that the rest of the method was skipped as 
    well as the fact that it also ignored @Before and @After in addition to @Test if
     assumeTrue is placed in the @BeforeClass method. The documentation doesn't 
    specify that sort of behavior (which I checked before asking the question).
    

    To answer this question, its not at all surprising. @BeforeClass is the first method to run while setting up your test case. Its a static setUp method thats called once to perform setUp for all your test cases. So if you use assumeTrue() here and it returns false, it throws an assumptionViolationException and halts everything else that's gonna happen. Thus nothing works for you. Makes sense?

    0 讨论(0)
提交回复
热议问题