...13import org.junit.Rule;14import org.junit.Test;15import org.junit.rules.MethodRule;16import org.junit.rules.TestName;17import org.junit.rules.TestWatchman;18import org.junit.runner.JUnitCore;19import org.junit.runner.Result;20import org.junit.runners.model.FrameworkMethod;21import org.junit.runners.model.Statement;2223public class RulesTest {24 private static boolean wasRun;2526 public static class ExampleTest {27 @Rule28 public MethodRule example= new MethodRule() {29 public Statement apply(final Statement base,30 FrameworkMethod method, Object target) {31 return new Statement() {32 @Override33 public void evaluate() throws Throwable {34 wasRun= true;35 base.evaluate();36 };37 };38 }39 };4041 @Test42 public void nothing() {4344 }45 }4647 @Test48 public void ruleIsIntroducedAndEvaluated() {49 wasRun= false;50 JUnitCore.runClasses(ExampleTest.class);51 assertTrue(wasRun);52 }5354 private static int runCount;5556 public static class MultipleRuleTest {57 private static class Increment implements MethodRule {58 public Statement apply(final Statement base,59 FrameworkMethod method, Object target) {60 return new Statement() {61 @Override62 public void evaluate() throws Throwable {63 runCount++;64 base.evaluate();65 };66 };67 }68 }6970 @Rule71 public MethodRule incrementor1= new Increment();7273 @Rule74 public MethodRule incrementor2= new Increment();7576 @Test77 public void nothing() {7879 }80 }8182 @Test83 public void multipleRulesAreRun() {84 runCount= 0;85 JUnitCore.runClasses(MultipleRuleTest.class);86 assertEquals(2, runCount);87 }8889 public static class NoRulesTest {90 public int x;9192 @Test93 public void nothing() {9495 }96 }9798 @Test99 public void ignoreNonRules() {100 Result result= JUnitCore.runClasses(NoRulesTest.class);101 assertEquals(0, result.getFailureCount());102 }103104 private static String log;105106 public static class OnFailureTest {107 @Rule108 public MethodRule watchman= new TestWatchman() {109 @Override110 public void failed(Throwable e, FrameworkMethod method) {111 log+= method.getName() + " " + e.getClass().getSimpleName();112 }113 };114115 @Test116 public void nothing() {117 fail();118 }119 }120121 @Test122 public void onFailure() {123 log= "";124 Result result= JUnitCore.runClasses(OnFailureTest.class);125 assertEquals("nothing AssertionError", log);126 assertEquals(1, result.getFailureCount());127 }128129 public static class WatchmanTest {130 private static String watchedLog;131132 @Rule133 public MethodRule watchman= new TestWatchman() {134 @Override135 public void failed(Throwable e, FrameworkMethod method) {136 watchedLog+= method.getName() + " "137 + e.getClass().getSimpleName() + "\n";138 }139140 @Override141 public void succeeded(FrameworkMethod method) {142 watchedLog+= method.getName() + " " + "success!\n";143 }144 };145146 @Test147 public void fails() {148 fail();149 }150151 @Test152 public void succeeds() {153 }154 }155156 @Test157 public void succeeded() {158 WatchmanTest.watchedLog= "";159 JUnitCore.runClasses(WatchmanTest.class);160 assertThat(WatchmanTest.watchedLog, containsString("fails AssertionError"));161 assertThat(WatchmanTest.watchedLog, containsString("succeeds success!"));162 }163164 public static class BeforesAndAfters {165 private static String watchedLog;166167 @Before public void before() {168 watchedLog+= "before ";169 }170 171 @Rule172 public MethodRule watchman= new TestWatchman() {173 @Override174 public void starting(FrameworkMethod method) {175 watchedLog+= "starting ";176 }177 178 @Override179 public void finished(FrameworkMethod method) {180 watchedLog+= "finished ";181 }182 183 @Override184 public void succeeded(FrameworkMethod method) {185 watchedLog+= "succeeded ";186 }
...