public
Last active

JUnit rule to conditionally ignore test cases (see also http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/)

  • Download Gist
ConditionalIgnoreRule
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/*******************************************************************************
* 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* RĂ¼diger Herrmann - initial API and implementation
* Matt Morrissette - allow to use non-static inner IgnoreConditions
******************************************************************************/
package com.codeaffine.gitplus.ui.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();
}
 
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ConditionalIgnore {
Class<? extends IgnoreCondition> condition();
}
 
@Override
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 ) {
this.target = target;
this.conditionType = annotation.condition();
}
 
IgnoreCondition create() {
checkConditionType();
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;
}
 
@Override
public void evaluate() {
Assume.assumeTrue( "Ignored by " + condition.getClass().getSimpleName(), false );
}
}
 
}

Does this work with any JUnit4? I am trying this with JUnit4.11, and the exact condition you described at http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/ All I get is: java.lang.RuntimeException: java.lang.InstantiationException: xyzTest$NotRunningOnWindows
at com.codeaffine.junit.ignore.ConditionalIgnoreRule.newCondition(ConditionalIgnoreRule.java:61)
at com.codeaffine.junit.ignore.ConditionalIgnoreRule.getIgnoreContition(ConditionalIgnoreRule.java:52)
at com.codeaffine.junit.ignore.ConditionalIgnoreRule.apply(ConditionalIgnoreRule.java:38)
at org.junit.runners.BlockJUnit4ClassRunner.withMethodRules(BlockJUnit4ClassRunner.java:349)
at org.junit.runners.BlockJUnit4ClassRunner.withRules(BlockJUnit4ClassRunner.java:339)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:256)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.InstantiationException: net.sourceforge.genieos.demos.WindowsLaunchTest$NotRunningOnWindows
at java.lang.Class.newInstance0(Class.java:359)
at java.lang.Class.newInstance(Class.java:327)
at com.codeaffine.junit.ignore.ConditionalIgnoreRule.newCondition(ConditionalIgnoreRule.java:57)
... 21 more

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: https://sourceforge.net/p/genieos/code-0/HEAD/tree/trunk/demos/src/test/java/net/sourceforge/genieos/demos/WindowsLaunchTest.java
Any idea?
TIA.

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

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

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: https://gist.github.com/yinzara/9980184

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.