Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
package com.[my package].rule;
import org.junit.Assume;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import timber.log.Timber;
import java.lang.annotation.*;
import java.lang.reflect.Modifier;
* Cobbled together from:
public class ConditionalIgnoreRule implements TestRule {
public interface IgnoreCondition {
boolean isSatisfied();
public @interface ConditionalIgnore {
Class<? extends IgnoreCondition> condition();
public Statement apply(Statement aStatement, Description aDescription) {
Statement result = aStatement;
if (hasConditionalIgnoreAnnotation(aDescription)) {
IgnoreCondition condition = getIgnoreCondition(aDescription);
if (condition.isSatisfied()) {
result = new IgnoreStatement(condition);
return result;
private static boolean hasConditionalIgnoreAnnotation( Description aDescription ) {
return aDescription.getAnnotation(ConditionalIgnore.class) != null;
private static IgnoreCondition getIgnoreCondition( Description aDescription ) {
ConditionalIgnore annotation = aDescription.getAnnotation(ConditionalIgnore.class);
return new IgnoreConditionCreator(aDescription.getTestClass(), annotation ).create();
private static class IgnoreConditionCreator {
private final Class<?> mTestClass;
private final Class<? extends IgnoreCondition> conditionType;
IgnoreConditionCreator(Class<?> aTestClass, ConditionalIgnore annotation) {
this.mTestClass = aTestClass;
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(mTestClass).newInstance(mTestClass);
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 mTestClass.getClass().isAssignableFrom(conditionType.getDeclaringClass());
private static class IgnoreStatement extends Statement {
private IgnoreCondition condition;
IgnoreStatement(IgnoreCondition condition) {
this.condition = condition;
public void evaluate() {
Timber.d("Ignored by " + condition.getClass().getSimpleName());
//TODO: Figure out how to actually have the android tests mark this as ignored instead passing.
import org.junit.Test;
import org.junit.runner.RunWith;
import com.[my package].rule.ConditionalIgnoreRule;
import com.[my package].rule.MustRunOnStagingRule;
//This is an example of a test I want to run only on staging.
public class CreditCardAPITests
//This must be added at the top of a test class or alternatively at the top of your base test class
//(which should not inherit from anything).
public final ConditionalIgnoreRule mConditionalIgnore = new ConditionalIgnoreRule();
@ConditionalIgnoreRule.ConditionalIgnore(condition = MustRunOnStagingRule.class)
public void addFakeMastercard() {
//Do stuff to add a fake mastercard
package com.[my package].rule;
import com.[my package].BuildConfig;
public class MustRunOnStagingRule implements ConditionalIgnoreRule.IgnoreCondition {
public boolean isSatisfied() {
//If the build must run on staging, this should satisfy if the build_type is NOT staging.
return !BuildConfig.BUILD_TYPE.equals("staging");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment