Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommend order attribute for TestWatcher and tidy up tests #1501

Merged
merged 2 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/org/junit/rules/TestWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.junit.AssumptionViolatedException;
import org.junit.Rule;
import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
Expand All @@ -17,7 +18,7 @@
* public static class WatchmanTest {
* private static String watchedLog;
*
* @Rule
* @Rule(order = Integer.MIN_VALUE)
* public TestWatcher watchman= new TestWatcher() {
* @Override
* protected void failed(Throwable e, Description description) {
Expand All @@ -40,6 +41,11 @@
* }
* }
* </pre>
* <p>It is recommended to always set the {@link Rule#order() order} of the
* {@code TestWatcher} to {@code Integer.MIN_VALUE} so that it encloses all
* other rules. Otherwise it may see failed tests as successful and vice versa
* if some rule changes the result of a test (e.g. {@link ErrorCollector} or
* {@link ExpectedException}).
*
* @since 4.9
*/
Expand Down
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