From 9cacd0b110551ba0ff3642f6c0d6776a27f364ce Mon Sep 17 00:00:00 2001 From: Stefan Birkner Date: Fri, 9 Feb 2018 10:21:48 +0100 Subject: [PATCH] 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. --- .../org/junit/rules/LoggingTestWatcher.java | 5 + .../java/org/junit/rules/TestRuleTest.java | 153 ------- .../java/org/junit/rules/TestWatcherTest.java | 403 ++++++++++++------ 3 files changed, 281 insertions(+), 280 deletions(-) diff --git a/src/test/java/org/junit/rules/LoggingTestWatcher.java b/src/test/java/org/junit/rules/LoggingTestWatcher.java index f1c0ddafba9b4..44d32a3e5a2c0 100644 --- a/src/test/java/org/junit/rules/LoggingTestWatcher.java +++ b/src/test/java/org/junit/rules/LoggingTestWatcher.java @@ -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 "); diff --git a/src/test/java/org/junit/rules/TestRuleTest.java b/src/test/java/org/junit/rules/TestRuleTest.java index 26a6e58bbb409..55fbb85156ae2 100644 --- a/src/test/java/org/junit/rules/TestRuleTest.java +++ b/src/test/java/org/junit/rules/TestRuleTest.java @@ -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(); @@ -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; diff --git a/src/test/java/org/junit/rules/TestWatcherTest.java b/src/test/java/org/junit/rules/TestWatcherTest.java index 100a13f540c86..3944cbf8c7839 100644 --- a/src/test/java/org/junit/rules/TestWatcherTest.java +++ b/src/test/java/org/junit/rules/TestWatcherTest.java @@ -1,192 +1,341 @@ package org.junit.rules; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.*; import static org.junit.Assume.assumeTrue; import static org.junit.experimental.results.PrintableResult.testResult; import static org.junit.experimental.results.ResultMatchers.failureCountIs; import static org.junit.experimental.results.ResultMatchers.hasFailureContaining; -import static org.junit.runner.JUnitCore.runClasses; + import org.junit.Rule; import org.junit.Test; import org.junit.experimental.results.PrintableResult; +import org.junit.experimental.runners.Enclosed; import org.junit.internal.AssumptionViolatedException; import org.junit.runner.Description; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; +import org.junit.runners.model.Statement; -public class TestWatcherTest { - public static class ViolatedAssumptionTest { - private static StringBuilder watchedLog = new StringBuilder(); +import java.util.List; - @Rule - public TestRule watcher = new LoggingTestWatcher(watchedLog); +@RunWith(Enclosed.class) +public class TestWatcherTest { - @Test - public void succeeds() { - assumeTrue(false); + @RunWith(Parameterized.class) + public static class Callbacks { + private static class NoOpRule implements TestRule { + public Statement apply(Statement base, Description description) { + return base; + } } - } - @Test - public void neitherLogSuccessNorFailedForViolatedAssumption() { - ViolatedAssumptionTest.watchedLog = new StringBuilder(); - runClasses(ViolatedAssumptionTest.class); - assertThat(ViolatedAssumptionTest.watchedLog.toString(), - is("starting skipped finished ")); - } + private static class ErroneousTestWatcher extends TestWatcher { + @Override + protected void succeeded(Description description) { + throw new RuntimeException("succeeded failed"); + } + + @Override + protected void failed(Throwable e, Description description) { + throw new RuntimeException("failed failed"); + } - public static class InternalViolatedAssumptionTest { - private static StringBuilder watchedLog = new StringBuilder(); + @Override + protected void skipped(org.junit.AssumptionViolatedException e, Description description) { + throw new RuntimeException("skipped failed"); + } + + @Override + protected void skipped(AssumptionViolatedException e, Description description) { + throw new RuntimeException("deprecated skipped failed"); + } - @Rule - public TestRule watcher = new TestWatcher() { @Override protected void starting(Description description) { - watchedLog.append("starting "); + throw new RuntimeException("starting failed"); } @Override protected void finished(Description description) { - watchedLog.append("finished "); + throw new RuntimeException("finished failed"); } + } - @Override - protected void skipped(AssumptionViolatedException e, Description description) { - watchedLog.append("skipped "); + private static TestRule selectedRule; + + public static class FailingTest { + @Rule + public TestRule rule = selectedRule; + + @Test + public void test() { + fail("test failed"); } - }; + } - @SuppressWarnings("deprecation") - @Test - public void succeeds() { - throw new AssumptionViolatedException("don't run"); + public static class InternalViolatedAssumptionTest { + @Rule + public TestRule watcher = selectedRule; + + @SuppressWarnings("deprecation") + @Test + public void test() { + throw new AssumptionViolatedException("don't run"); + } } - } - @Test - public void internalViolatedAssumption() { - InternalViolatedAssumptionTest.watchedLog = new StringBuilder(); - runClasses(InternalViolatedAssumptionTest.class); - assertThat(InternalViolatedAssumptionTest.watchedLog.toString(), - is("starting skipped finished ")); - } + public static class SuccessfulTest { + @Rule + public TestRule watcher = selectedRule; - public static class TestWatcherSkippedThrowsExceptionTest { - @Rule - public TestRule watcher = new TestWatcher() { - @Override - protected void skipped(AssumptionViolatedException e, Description description) { - throw new RuntimeException("watcher failure"); + @Test + public void test() { + } + } + + public static class ViolatedAssumptionTest { + @Rule + public TestRule watcher = selectedRule; + + @Test + public void test() { + assumeTrue(false); } - }; + } + + @Parameters(name = "{0}") + public static Object[][] parameters() { + return new Object[][] { + new Object[] { + FailingTest.class, + "starting failed finished ", + asList("starting failed", "test failed", "failed failed", "finished failed") }, + new Object[] { + InternalViolatedAssumptionTest.class, + "starting deprecated skipped finished ", + asList("starting failed", "don't run", "deprecated skipped failed", "finished failed") }, + new Object[] { + SuccessfulTest.class, + "starting succeeded finished ", + asList("starting failed", "succeeded failed", "finished failed") }, + new Object[] { + ViolatedAssumptionTest.class, + "starting skipped finished ", + asList("starting failed", "Test could not be skipped due to other failures", "skipped failed", "finished failed") } + }; + } + + @Parameter(0) + public Class testClass; + + @Parameter(1) + public String expectedCallbacks; + + @Parameter(2) + public List expectedFailures; - @SuppressWarnings("deprecation") @Test - public void fails() { - throw new AssumptionViolatedException("test failure"); + public void correctCallbacksCalled() { + StringBuilder log = new StringBuilder(); + selectedRule = new LoggingTestWatcher(log); + JUnitCore.runClasses(testClass); + assertEquals(expectedCallbacks, log.toString()); } - } - @Test - public void testWatcherSkippedThrowsException() { - PrintableResult result = testResult(TestWatcherSkippedThrowsExceptionTest.class); - assertThat(result, failureCountIs(2)); - assertThat(result, hasFailureContaining("test failure")); - assertThat(result, hasFailureContaining("watcher failure")); + @Test + public void resultHasAllFailuresThrownByCallbacks() { + selectedRule = new ErroneousTestWatcher(); + PrintableResult result = testResult(testClass); + assertThat(result, failureCountIs(expectedFailures.size())); + for (String expectedFailure: expectedFailures) { + assertThat(result, hasFailureContaining(expectedFailure)); + } + } + + @Test + public void testWatcherDoesNotModifyResult() { + selectedRule = new NoOpRule(); + Result resultNoOpRule = JUnitCore.runClasses(testClass); + selectedRule = new LoggingTestWatcher(new StringBuilder()); + Result resultTestWatcher = JUnitCore.runClasses(testClass); + assertEquals( + "was successful", + resultNoOpRule.wasSuccessful(), + resultTestWatcher.wasSuccessful()); + assertEquals( + "failure count", + resultNoOpRule.getFailureCount(), + resultTestWatcher.getFailureCount()); + assertEquals( + "ignore count", + resultNoOpRule.getIgnoreCount(), + resultTestWatcher.getIgnoreCount()); + assertEquals( + "run count", + resultNoOpRule.getRunCount(), + resultTestWatcher.getRunCount()); + } } - public static class FailingTest { - private static StringBuilder watchedLog = new StringBuilder(); + public static class CallbackArguments { + + public static class Succeeded { + private static Description catchedDescription; - @Rule - public TestRule watcher = new LoggingTestWatcher(watchedLog); + @Rule + public final TestRule watcher = new TestWatcher() { + @Override + protected void succeeded(Description description) { + catchedDescription = description; + } + }; + + @Test + public void test() { + } + } @Test - public void succeeds() { - fail(); + public void succeeded() { + JUnitCore.runClasses(Succeeded.class); + assertEquals("test(org.junit.rules.TestWatcherTest$CallbackArguments$Succeeded)", + Succeeded.catchedDescription.getDisplayName()); } - } - @Test - public void logFailingTest() { - FailingTest.watchedLog = new StringBuilder(); - runClasses(FailingTest.class); - assertThat(FailingTest.watchedLog.toString(), - is("starting failed finished ")); - } + public static class Failed { + private static Description catchedDescription; + private static Throwable catchedThrowable; - public static class TestWatcherFailedThrowsExceptionTest { - @Rule - public TestRule watcher = new TestWatcher() { - @Override - protected void failed(Throwable e, Description description) { - throw new RuntimeException("watcher failure"); + @Rule + public final TestRule watcher = new TestWatcher() { + @Override + protected void failed(Throwable e, Description description) { + catchedDescription = description; + catchedThrowable = e; + } + }; + + @Test + public void test() { + fail("test failed"); } - }; + } @Test - public void fails() { - throw new IllegalArgumentException("test failure"); + public void failed() { + JUnitCore.runClasses(Failed.class); + assertEquals("test failed", Failed.catchedThrowable.getMessage()); + assertEquals(AssertionError.class, Failed.catchedThrowable.getClass()); + assertEquals("test(org.junit.rules.TestWatcherTest$CallbackArguments$Failed)", + Failed.catchedDescription.getDisplayName()); } - } - @Test - public void testWatcherFailedThrowsException() { - PrintableResult result = testResult(TestWatcherFailedThrowsExceptionTest.class); - assertThat(result, failureCountIs(2)); - assertThat(result, hasFailureContaining("test failure")); - assertThat(result, hasFailureContaining("watcher failure")); - } + public static class Skipped { + private static Description catchedDescription; + private static org.junit.AssumptionViolatedException catchedException; - public static class TestWatcherStartingThrowsExceptionTest { - @Rule - public TestRule watcher = new TestWatcher() { - @Override - protected void starting(Description description) { - throw new RuntimeException("watcher failure"); + @Rule + public final TestRule watcher = new TestWatcher() { + @Override + protected void skipped(org.junit.AssumptionViolatedException e, Description description) { + catchedDescription = description; + catchedException = e; + } + }; + + @Test + public void test() { + assumeTrue("test skipped", false); } - }; + } @Test - public void fails() { - throw new IllegalArgumentException("test failure"); + public void skipped() { + JUnitCore.runClasses(Skipped.class); + assertEquals("test skipped", Skipped.catchedException.getMessage()); + assertEquals(org.junit.AssumptionViolatedException.class, Skipped.catchedException.getClass()); + assertEquals("test(org.junit.rules.TestWatcherTest$CallbackArguments$Skipped)", + Skipped.catchedDescription.getDisplayName()); } - } - @Test - public void testWatcherStartingThrowsException() { - PrintableResult result = testResult(TestWatcherStartingThrowsExceptionTest.class); - assertThat(result, failureCountIs(2)); - assertThat(result, hasFailureContaining("test failure")); - assertThat(result, hasFailureContaining("watcher failure")); - } + public static class DeprecatedSkipped { + private static Description catchedDescription; + private static AssumptionViolatedException catchedException; - public static class TestWatcherFailedAndFinishedThrowsExceptionTest { - @Rule - public TestRule watcher = new TestWatcher() { - @Override - protected void failed(Throwable e, Description description) { - throw new RuntimeException("watcher failed failure"); + @Rule + public final TestRule watcher = new TestWatcher() { + @Override + protected void skipped(AssumptionViolatedException e, Description description) { + catchedDescription = description; + catchedException = e; + } + }; + + @Test + public void test() { + throw new AssumptionViolatedException("test skipped"); } + } - @Override - protected void finished(Description description) { - throw new RuntimeException("watcher finished failure"); + @Test + public void deprecatedSkipped() { + JUnitCore.runClasses(DeprecatedSkipped.class); + assertEquals("test skipped", DeprecatedSkipped.catchedException.getMessage()); + assertEquals(AssumptionViolatedException.class, DeprecatedSkipped.catchedException.getClass()); + assertEquals("test(org.junit.rules.TestWatcherTest$CallbackArguments$DeprecatedSkipped)", + DeprecatedSkipped.catchedDescription.getDisplayName()); + } + + public static class Starting { + private static Description catchedDescription; + + @Rule + public final TestRule watcher = new TestWatcher() { + @Override + protected void starting(Description description) { + catchedDescription = description; + } + }; + + @Test + public void test() { } - }; + } @Test - public void fails() { - throw new IllegalArgumentException("test failure"); + public void starting() { + JUnitCore.runClasses(Starting.class); + assertEquals("test(org.junit.rules.TestWatcherTest$CallbackArguments$Starting)", + Starting.catchedDescription.getDisplayName()); } - } - @Test - public void testWatcherFailedAndFinishedThrowsException() { - PrintableResult result = testResult(TestWatcherFailedAndFinishedThrowsExceptionTest.class); - assertThat(result, failureCountIs(3)); - assertThat(result, hasFailureContaining("test failure")); - assertThat(result, hasFailureContaining("watcher failed failure")); - assertThat(result, hasFailureContaining("watcher finished failure")); + public static class Finished { + private static Description catchedDescription; + + @Rule + public final TestRule watcher = new TestWatcher() { + @Override + protected void finished(Description description) { + catchedDescription = description; + } + }; + + @Test + public void test() { + } + } + + @Test + public void finished() { + JUnitCore.runClasses(Finished.class); + assertEquals("test(org.junit.rules.TestWatcherTest$CallbackArguments$Finished)", + Finished.catchedDescription.getDisplayName()); + } } }