Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.