Skip to content

Commit 715a445

Browse files
committed
Tidy up tests for TestWatcher
- Make it easier to understand TestWatcher's behavior based on the tests. - Improve expressiveness of the test. - Add missing tests.
1 parent 1b4ac07 commit 715a445

File tree

3 files changed

+284
-280
lines changed

3 files changed

+284
-280
lines changed

src/test/java/org/junit/rules/LoggingTestWatcher.java

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ protected void skipped(AssumptionViolatedException e, Description description) {
2525
log.append("skipped ");
2626
}
2727

28+
@Override
29+
protected void skipped(org.junit.internal.AssumptionViolatedException e, Description description) {
30+
log.append("deprecated skipped ");
31+
}
32+
2833
@Override
2934
protected void starting(Description description) {
3035
log.append("starting ");

src/test/java/org/junit/rules/TestRuleTest.java

-153
Original file line numberDiff line numberDiff line change
@@ -148,64 +148,6 @@ public void ignoreNonRules() {
148148

149149
private static String log;
150150

151-
public static class OnFailureTest {
152-
@Rule
153-
public TestRule watcher = new TestWatcher() {
154-
@Override
155-
protected void failed(Throwable e, Description description) {
156-
log += description + " " + e.getClass().getSimpleName();
157-
}
158-
};
159-
160-
@Test
161-
public void nothing() {
162-
fail();
163-
}
164-
}
165-
166-
@Test
167-
public void onFailure() {
168-
log = "";
169-
Result result = JUnitCore.runClasses(OnFailureTest.class);
170-
assertEquals(String.format("nothing(%s) AssertionError", OnFailureTest.class.getName()), log);
171-
assertEquals(1, result.getFailureCount());
172-
}
173-
174-
public static class WatchmanTest {
175-
private static String watchedLog;
176-
177-
@Rule
178-
public TestRule watcher = new TestWatcher() {
179-
@Override
180-
protected void failed(Throwable e, Description description) {
181-
watchedLog += description + " "
182-
+ e.getClass().getSimpleName() + "\n";
183-
}
184-
185-
@Override
186-
protected void succeeded(Description description) {
187-
watchedLog += description + " " + "success!\n";
188-
}
189-
};
190-
191-
@Test
192-
public void fails() {
193-
fail();
194-
}
195-
196-
@Test
197-
public void succeeds() {
198-
}
199-
}
200-
201-
@Test
202-
public void succeeded() {
203-
WatchmanTest.watchedLog = "";
204-
JUnitCore.runClasses(WatchmanTest.class);
205-
assertThat(WatchmanTest.watchedLog, containsString(String.format("fails(%s) AssertionError", WatchmanTest.class.getName())));
206-
assertThat(WatchmanTest.watchedLog, containsString(String.format("succeeds(%s) success!", WatchmanTest.class.getName())));
207-
}
208-
209151
public static class BeforesAndAfters {
210152
private static StringBuilder watchedLog = new StringBuilder();
211153

@@ -437,101 +379,6 @@ public void methodIgnoreNonRules() {
437379
assertEquals(0, result.getFailureCount());
438380
}
439381

440-
public static class MethodOnFailureTest {
441-
private TestRule watchman = new TestWatcher() {
442-
@Override
443-
protected void failed(Throwable e, Description description) {
444-
log += description + " " + e.getClass().getSimpleName();
445-
}
446-
};
447-
448-
@Rule
449-
public TestRule getWatchman() {
450-
return watchman;
451-
}
452-
453-
@Test
454-
public void nothing() {
455-
fail();
456-
}
457-
}
458-
459-
@Test
460-
public void methodOnFailure() {
461-
log = "";
462-
Result result = JUnitCore.runClasses(MethodOnFailureTest.class);
463-
assertEquals(String.format("nothing(%s) AssertionError", MethodOnFailureTest.class.getName()), log);
464-
assertEquals(1, result.getFailureCount());
465-
}
466-
467-
public static class MethodOnSkippedTest {
468-
private TestRule watchman = new TestWatcher() {
469-
@Override
470-
protected void skipped(AssumptionViolatedException e, Description description) {
471-
log += description + " " + e.getClass().getSimpleName();
472-
}
473-
};
474-
475-
@Rule
476-
public TestRule getWatchman() {
477-
return watchman;
478-
}
479-
480-
@Test
481-
public void nothing() {
482-
Assume.assumeTrue(false);
483-
}
484-
}
485-
486-
@Test
487-
public void methodOnSkipped() {
488-
log = "";
489-
Result result = JUnitCore.runClasses(MethodOnSkippedTest.class);
490-
assertEquals(String.format("nothing(%s) AssumptionViolatedException", MethodOnSkippedTest.class.getName()), log);
491-
assertEquals(0, result.getFailureCount());
492-
assertEquals(1, result.getRunCount());
493-
}
494-
495-
public static class MethodWatchmanTest {
496-
@SuppressWarnings("unused")
497-
private static String watchedLog;
498-
499-
private TestRule watchman = new TestWatcher() {
500-
@Override
501-
protected void failed(Throwable e, Description description) {
502-
watchedLog += description + " "
503-
+ e.getClass().getSimpleName() + "\n";
504-
}
505-
506-
@Override
507-
protected void succeeded(Description description) {
508-
watchedLog += description + " " + "success!\n";
509-
}
510-
};
511-
512-
@Rule
513-
public TestRule getWatchman() {
514-
return watchman;
515-
}
516-
517-
@Test
518-
public void fails() {
519-
fail();
520-
}
521-
522-
@Test
523-
public void succeeds() {
524-
}
525-
}
526-
527-
@Test
528-
public void methodSucceeded() {
529-
WatchmanTest.watchedLog = "";
530-
JUnitCore.runClasses(WatchmanTest.class);
531-
assertThat(WatchmanTest.watchedLog, containsString(String.format("fails(%s) AssertionError", WatchmanTest.class.getName())));
532-
assertThat(WatchmanTest.watchedLog, containsString(String.format("succeeds(%s) success!", WatchmanTest.class.getName())));
533-
}
534-
535382
public static class BeforesAndAftersAreEnclosedByRule {
536383
private static StringBuilder log;
537384

0 commit comments

Comments
 (0)