Skip to content

Instantly share code, notes, and snippets.

@RainerW
Created June 25, 2012 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RainerW/2991540 to your computer and use it in GitHub Desktop.
Save RainerW/2991540 to your computer and use it in GitHub Desktop.
JUnit Ignore Rule sample
package com.example.junit116;
import java.util.ArrayList;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import static org.fest.assertions.Assertions.*;
public class Test4JUnitIssue_116_IgnoreRules {
List<String> ruleMethodExceutions = new ArrayList<String>();
@Rule
public IgnoreRule ignore = new IgnoreRule();
@Rule
public SomeRule outer = new SomeRule();
@Test
public void checkOrder() {
assertThat(ruleMethodExceutions)
.containsSequence("IgnoreRule apply","SomeRule apply","SomeRule evaluate","IgnoreRule evaluate");
}
@Test
public void ignored() {
// Log will not contain anything for this testmethod
}
class SomeRule implements MethodRule {
@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
ruleMethodExceutions.add("SomeRule apply");
return new Statement() {
@Override
public void evaluate() throws Throwable {
ruleMethodExceutions.add("SomeRule evaluate");
System.out.println("SomeRule evaluate()");
base.evaluate();
}
};
}
}
class IgnoreRule implements MethodRule {
@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
ruleMethodExceutions.add("IgnoreRule apply");
if(method.getMethod().getName().equals("ignored")) {
throw new AssumptionViolatedException("ignored");
}
return new Statement() {
@Override
public void evaluate() throws Throwable {
ruleMethodExceutions.add("IgnoreRule evaluate");
System.out.println("IgnoreRule evaluate()");
base.evaluate();
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment