Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore unit test when condition meets?

I was wondering if there is an annotation or way to only execute test if pre-conditoin meets? I have a situation where some tests are relevant until a specific date is met. I use JUnit, Mockito. Thanks

like image 934
user_1357 Avatar asked Oct 10 '13 16:10

user_1357


2 Answers

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.

like image 88
Vishal Shukla Avatar answered Sep 25 '22 21:09

Vishal Shukla


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.

like image 21
Brian Agnew Avatar answered Sep 23 '22 21:09

Brian Agnew