Search code examples
junitassert

Using Assume.assumeTrue or Assert.assertTrue, when do tests exit?


I am using JUnit 4.12 and this is my current understanding of the following APIs I use frequently:

  • assumeTrue: if expression evaluates to false, test will halt and be ignored
  • assertTrue: if condition is false, throws an AssertionError
  • assertEquals: if they are not equal, an AssertionError is thrown with the given message
  • assertNotNull: if it is null, throws an AssertionError

However, I am unable to get clarity on couple of things:

  1. my understanding is that only for assumeTrue, the tests will exit but the very definition of assert is that the program should exit when the statement evaluates to false
  2. when the test throws an AssertionError, does it exit the test case or continue executing the remaining steps?
  3. can the tests be considered pass, even if they throw an AssertionError or are the tests considered fail?

Solution

    1. assumeTrue means that the test shouldn't run. It does not mean your code is broken and most runners will treat this test as "ignored"

    2. An AssertionError means the test has failed. No more steps in that test case (the single method) will be run. It would be pointless to do so as the test has already failed

    3. The test has failed. If you want to negate the meaning of a test there are other ways to do that, e.g. replace assertNull with assertNotNull