Software QA FYI - SQAFYI

JUnit Rule to Conditionally Ignore Tests

By: Rüdiger Herrmann

I always believed that using @Ignore to deactivate tests is a bad idea. Except, maybe as one way to put tests that fail intermittently into quarantine to attend to them later (as Martin Fowler describes it here). This bears the danger that the test suite decays as more and more tests keep getting ignored and forgotten. Therefore you should have a policy in place to ensure that tests aren’t quarantined for too long. Well, so I thought until recently when we ran into this:

In a project that Frank and I work on, we ran into an SWT issue described here. On non-Windows platforms, asserting whether an SWT widget has got the input focus does not work with automated tests.

We decided to ignore focus-related tests on non-Windows platforms for now. Though our build server runs on Linux we found it safe enough as both our development environments run on Windows.

In JUnit, assumptions are the means to skip tests that aren’t meaningful under the given condition. Expressed that way, our test would look like this:
1 public void testFocus() {
2 assumeTrue( isRunningOnWindows() );
3 // ...
4 }

But we didn’t want the test code mingled with conditions if it should be executed at all. The code that decides whether the test is ignored should be separated from the test code itself.

This led us to creating a ConditionalIgnore annotation and a corresponding rule to hook it into the JUnit runtime. The thing is simple and best explained with an example:
01 public class SomeTest {
02 @Rule
03 public ConditionalIgnoreRule rule = new ConditionalIgnoreRule();
05 @Test
06 @ConditionalIgnore( condition = NotRunningOnWindows.class )
07 public void testFocus() {
08 // ...
09 }
10 }
12 public class NotRunningOnWindows implements IgnoreCondition {
13 public boolean isSatisfied() {
14 return !System.getProperty( "" ).startsWith( "Windows" );
15 }
16 }

The ConditionalIgnore annotation requires a ‘condition’ property that points to a class that implements IgnoreContition. At runtime, an instance of the IgnoreCondition implementation is created and its isSatisfied() method decides whether the test is ignored (returns true) or not (returns false). Finally there is an IgnoreConditionRule that hooks the annotations into the JUnit runtime.

Full article...

Other Resource

... to read more articles, visit

JUnit Rule to Conditionally Ignore Tests