Skip to content

Commit

Permalink
Use LoggingTestWatcher (DRY)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbirkner committed Feb 8, 2018
1 parent 43cdf99 commit 1b4ac07
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions src/test/java/org/junit/rules/TestRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,51 +533,33 @@ public void methodSucceeded() {
}

public static class BeforesAndAftersAreEnclosedByRule {
private static String watchedLog;
private static StringBuilder log;

@Rule
public TestRule watcher = new LoggingTestWatcher(log);

@Before
public void before() {
watchedLog += "before ";
}

private TestRule watchman = new TestWatcher() {
@Override
protected void starting(Description d) {
watchedLog += "starting ";
}

@Override
protected void finished(Description d) {
watchedLog += "finished ";
}

@Override
protected void succeeded(Description d) {
watchedLog += "succeeded ";
}
};

@Rule
public TestRule getWatchman() {
return watchman;
log.append("before ");
}

@After
public void after() {
watchedLog += "after ";
log.append("after ");
}

@Test
public void succeeds() {
watchedLog += "test ";
log.append("test ");
}
}

@Test
public void beforesAndAftersAreEnclosedByRule() {
BeforesAndAftersAreEnclosedByRule.watchedLog = "";
BeforesAndAftersAreEnclosedByRule.log = new StringBuilder();
JUnitCore.runClasses(BeforesAndAftersAreEnclosedByRule.class);
assertThat(BeforesAndAftersAreEnclosedByRule.watchedLog, is("starting before test after succeeded finished "));
assertEquals("starting before test after succeeded finished ",
BeforesAndAftersAreEnclosedByRule.log.toString());
}

public static class MethodWrongTypedField {
Expand Down

0 comments on commit 1b4ac07

Please sign in to comment.