public

JUnit Ignore Rule sample

  • Download Gist
Test4JUnitIssue_116_IgnoreRules.java
Java
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
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();
}
};
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.