Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
JUnit rule to conditionally ignore test cases (see also
* Copyright (c) 2013,2014 Rüdiger Herrmann
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* Contributors:
* Rüdiger Herrmann - initial API and implementation
* Matt Morrissette - allow to use non-static inner IgnoreConditions
package com.codeaffine.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Modifier;
import org.junit.Assume;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
public class ConditionalIgnoreRule implements MethodRule {
public interface IgnoreCondition {
boolean isSatisfied();
public @interface ConditionalIgnore {
Class<? extends IgnoreCondition> condition();
public Statement apply( Statement base, FrameworkMethod method, Object target ) {
Statement result = base;
if( hasConditionalIgnoreAnnotation( method ) ) {
IgnoreCondition condition = getIgnoreContition( target, method );
if( condition.isSatisfied() ) {
result = new IgnoreStatement( condition );
return result;
private static boolean hasConditionalIgnoreAnnotation( FrameworkMethod method ) {
return method.getAnnotation( ConditionalIgnore.class ) != null;
private static IgnoreCondition getIgnoreContition( Object target, FrameworkMethod method ) {
ConditionalIgnore annotation = method.getAnnotation( ConditionalIgnore.class );
return new IgnoreConditionCreator( target, annotation ).create();
private static class IgnoreConditionCreator {
private final Object target;
private final Class<? extends IgnoreCondition> conditionType;
IgnoreConditionCreator( Object target, ConditionalIgnore annotation ) { = target;
this.conditionType = annotation.condition();
IgnoreCondition create() {
try {
return createCondition();
} catch( RuntimeException re ) {
throw re;
} catch( Exception e ) {
throw new RuntimeException( e );
private IgnoreCondition createCondition() throws Exception {
IgnoreCondition result;
if( isConditionTypeStandalone() ) {
result = conditionType.newInstance();
} else {
result = conditionType.getDeclaredConstructor( target.getClass() ).newInstance( target );
return result;
private void checkConditionType() {
if( !isConditionTypeStandalone() && !isConditionTypeDeclaredInTarget() ) {
String msg
= "Conditional class '%s' is a member class "
+ "but was not declared inside the test case using it.\n"
+ "Either make this class a static class, "
+ "standalone class (by declaring it in it's own file) "
+ "or move it inside the test case using it";
throw new IllegalArgumentException( String.format ( msg, conditionType.getName() ) );
private boolean isConditionTypeStandalone() {
return !conditionType.isMemberClass() || Modifier.isStatic( conditionType.getModifiers() );
private boolean isConditionTypeDeclaredInTarget() {
return target.getClass().isAssignableFrom( conditionType.getDeclaringClass() );
private static class IgnoreStatement extends Statement {
private final IgnoreCondition condition;
IgnoreStatement( IgnoreCondition condition ) {
this.condition = condition;
public void evaluate() {
Assume.assumeTrue( "Ignored by " + condition.getClass().getSimpleName(), false );

SiKing commented Apr 2, 2014

Does this work with any JUnit4? I am trying this with JUnit4.11, and the exact condition you described at All I get is: java.lang.RuntimeException: java.lang.InstantiationException: xyzTest$NotRunningOnWindows
at com.codeaffine.junit.ignore.ConditionalIgnoreRule.newCondition(
at com.codeaffine.junit.ignore.ConditionalIgnoreRule.getIgnoreContition(
at com.codeaffine.junit.ignore.ConditionalIgnoreRule.apply(
at org.junit.runners.BlockJUnit4ClassRunner.withMethodRules(
at org.junit.runners.BlockJUnit4ClassRunner.withRules(
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
at org.junit.runners.ParentRunner$
at org.junit.runners.ParentRunner$1.schedule(
at org.junit.runners.ParentRunner.runChildren(
at org.junit.runners.ParentRunner.access$000(
at org.junit.runners.ParentRunner$2.evaluate(
at org.junit.internal.runners.statements.RunBefores.evaluate(
at org.junit.internal.runners.statements.RunAfters.evaluate(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
Caused by: java.lang.InstantiationException: net.sourceforge.genieos.demos.WindowsLaunchTest$NotRunningOnWindows
at java.lang.Class.newInstance0(
at java.lang.Class.newInstance(
at com.codeaffine.junit.ignore.ConditionalIgnoreRule.newCondition(
... 21 more

SiKing commented Apr 2, 2014

Upon further investigation I dicovered that having a @before setUp() causes this failure. If I remove the @conditionalignore from my test, it passes just fine. The setUp() - with the @conditionalignore - always fails.
My source is here:
Any idea?

SiKing commented Apr 3, 2014

Just tried to run this on my Linux machine: the @before setUp() is still run.
Am I using this wrong?

SiKing commented Apr 4, 2014

Found my issue: I declared the NotRunningOnWindows class inside of my SomeTest class, and so it has to be explicitly static.

yinzara commented Apr 4, 2014

I have fixed the above class to work with classes declared inside your test case (i.e. anonymous inner member classes). The fix is in my fork of this Gist available at:

smesh commented Dec 24, 2015 - ConditionalIgnore from Apache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment