How to skip a test in JUNIT using assumeTrue()

Hello Automation people,

Today we will be talking about a situation which may have occurred to most of the Automation testers.Sometimes while testing we might come across a scenario where we are relying on the existing data, in this case what should be done if the existing data is not present?We usually have two options:

  1. Mark the testcase FAIL as the testexecution never happened because of no data available for testing.
  2. Mark the testcase PASS as the testexecution never happened so no point of marking it as FAIL.

Now, i would like to tell you that both the above options are not correct, because marking a testcase FAIL or PASS without even executing the testcase is not right.

So what should be done now?

Dont worry guys we have JUNIT to help us in this situation. We can mark the testcase as skipped using JUNIT without using the @Ignore annotation.
Please see the below code:

If you observed the above code, it states that it is expecting the value of i to be greater than 0, which is not the case in the above testcase.Therefore when the condition is not satisfied it throws an exception named :AssumptionViolatedException.
This exception helps us to skip the test without having the need to mark it as FAIL or PASS which is the reason you have to let the JUNIT throw this exception.

Please see the below code:

The above test is going to PASS as JAVA has internally handled the exception, as i had said that the exception :AssumptionViolatedException helps us to skip the test so you have to throw the exception.

Watch the below code:

In the above code, we are checking if the exception occurred is an instance of AssumptionViolatedException, if yes then throw the exception which in turn marks the testcase as SKIPPED.
Hop you guys have understood on how to skip the testcase using assumeTrue.

Thanks guys, Enjoy automating.

Automate anything, Automate everything…!!