Skip to content

Commit

Permalink
Tidy up tests for TestWatcher
Browse files Browse the repository at this point in the history
- Make it easier to understand TestWatcher's behavior based on the tests.
- Improve expressiveness of the test.
- Add missing tests.
  • Loading branch information
stefanbirkner committed Feb 10, 2018
1 parent 1b4ac07 commit 9cacd0b
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 280 deletions.
5 changes: 5 additions & 0 deletions src/test/java/org/junit/rules/LoggingTestWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ protected void skipped(AssumptionViolatedException e, Description description) {
log.append("skipped ");
}

@Override
protected void skipped(org.junit.internal.AssumptionViolatedException e, Description description) {
log.append("deprecated skipped ");
}

@Override
protected void starting(Description description) {
log.append("starting ");
Expand Down
153 changes: 0 additions & 153 deletions src/test/java/org/junit/rules/TestRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,64 +148,6 @@ public void ignoreNonRules() {

private static String log;

public static class OnFailureTest {
@Rule
public TestRule watcher = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
log += description + " " + e.getClass().getSimpleName();
}
};

@Test
public void nothing() {
fail();
}
}

@Test
public void onFailure() {
log = "";
Result result = JUnitCore.runClasses(OnFailureTest.class);
assertEquals(String.format("nothing(%s) AssertionError", OnFailureTest.class.getName()), log);
assertEquals(1, result.getFailureCount());
}

public static class WatchmanTest {
private static String watchedLog;

@Rule
public TestRule watcher = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
watchedLog += description + " "
+ e.getClass().getSimpleName() + "\n";
}

@Override
protected void succeeded(Description description) {
watchedLog += description + " " + "success!\n";
}
};

@Test
public void fails() {
fail();
}

@Test
public void succeeds() {
}
}

@Test
public void succeeded() {
WatchmanTest.watchedLog = "";
JUnitCore.runClasses(WatchmanTest.class);
assertThat(WatchmanTest.watchedLog, containsString(String.format("fails(%s) AssertionError", WatchmanTest.class.getName())));
assertThat(WatchmanTest.watchedLog, containsString(String.format("succeeds(%s) success!", WatchmanTest.class.getName())));
}

public static class BeforesAndAfters {
private static StringBuilder watchedLog = new StringBuilder();

Expand Down Expand Up @@ -437,101 +379,6 @@ public void methodIgnoreNonRules() {
assertEquals(0, result.getFailureCount());
}

public static class MethodOnFailureTest {
private TestRule watchman = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
log += description + " " + e.getClass().getSimpleName();
}
};

@Rule
public TestRule getWatchman() {
return watchman;
}

@Test
public void nothing() {
fail();
}
}

@Test
public void methodOnFailure() {
log = "";
Result result = JUnitCore.runClasses(MethodOnFailureTest.class);
assertEquals(String.format("nothing(%s) AssertionError", MethodOnFailureTest.class.getName()), log);
assertEquals(1, result.getFailureCount());
}

public static class MethodOnSkippedTest {
private TestRule watchman = new TestWatcher() {
@Override
protected void skipped(AssumptionViolatedException e, Description description) {
log += description + " " + e.getClass().getSimpleName();
}
};

@Rule
public TestRule getWatchman() {
return watchman;
}

@Test
public void nothing() {
Assume.assumeTrue(false);
}
}

@Test
public void methodOnSkipped() {
log = "";
Result result = JUnitCore.runClasses(MethodOnSkippedTest.class);
assertEquals(String.format("nothing(%s) AssumptionViolatedException", MethodOnSkippedTest.class.getName()), log);
assertEquals(0, result.getFailureCount());
assertEquals(1, result.getRunCount());
}

public static class MethodWatchmanTest {
@SuppressWarnings("unused")
private static String watchedLog;

private TestRule watchman = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
watchedLog += description + " "
+ e.getClass().getSimpleName() + "\n";
}

@Override
protected void succeeded(Description description) {
watchedLog += description + " " + "success!\n";
}
};

@Rule
public TestRule getWatchman() {
return watchman;
}

@Test
public void fails() {
fail();
}

@Test
public void succeeds() {
}
}

@Test
public void methodSucceeded() {
WatchmanTest.watchedLog = "";
JUnitCore.runClasses(WatchmanTest.class);
assertThat(WatchmanTest.watchedLog, containsString(String.format("fails(%s) AssertionError", WatchmanTest.class.getName())));
assertThat(WatchmanTest.watchedLog, containsString(String.format("succeeds(%s) success!", WatchmanTest.class.getName())));
}

public static class BeforesAndAftersAreEnclosedByRule {
private static StringBuilder log;

Expand Down
Loading

0 comments on commit 9cacd0b

Please sign in to comment.