How to ignore unit test when condition meets?
You can do this by using Assume.
In below shown example, I want to check status in case if precondition==true
and I want to assert that exception is thrown in case of precondition==false
.
@Test
public final void testExecute() throws InvalidSyntaxException {
Assume.assumeTrue(precondition); // Further execution will be skipped if precondition holds false
CommandResult result = sentence.getCommand().execute();
boolean status = Boolean.parseBoolean(result.getResult());
Assert.assertTrue(status);
}
@Test(expected = InvalidSyntaxException.class)
public final void testInvalidParse() throws InvalidSyntaxException {
Assume.assumeTrue(!precondition);
CommandResult result = sentence.getCommand().execute();
}
Hope this helps to you.
You can use the Assume class
A set of methods useful for stating assumptions about the conditions in which a test is meaningful. A failed assumption does not mean the code is broken, but that the test provides no useful information. The default JUnit runner treats tests with failing assumptions as ignored.
so at the start of your test you can write
Assume.assumeThat("Condition not true - ignoreing test", myPreCondition);
and JUnit will ignore this test if myPreCondition
is false.