How to ignore unit test when a condition occurs?

I was wondering if there is an annotation or a way to run a test only if pre-conditoin is encountered? I have a situation where some tests are relevant until a specific date is reached. I am using JUnit, Mockito. Thanks

+6
source share
2 answers

You can do this with Assume.

In the example below, I want to check the status if precondition==true and want to confirm that an exception is precondition==false if 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 you.

+8
source

You can use the Assume class.

A set of methods useful for formulating assumptions about the conditions in which the test makes sense. An unsuccessful assumption does not mean that the code does not work, but the test does not contain any useful information. by default, the JUnit runner considers tests with errors that are ignored.

so at the beginning of the test you can write

 Assume.assumeThat("Condition not true - ignoreing test", myPreCondition); 

and JUnit will ignore this test if myPreCondition is false.

+5
source

Source: https://habr.com/ru/post/955687/


All Articles