diff --git a/src/test/java/org/junit/tests/AllTests.java b/src/test/java/org/junit/tests/AllTests.java index fd9169121d1ab..f6a229d4f6851 100644 --- a/src/test/java/org/junit/tests/AllTests.java +++ b/src/test/java/org/junit/tests/AllTests.java @@ -20,6 +20,7 @@ import org.junit.tests.manipulation.AllManipulationTests; import org.junit.tests.running.AllRunningTests; import org.junit.tests.validation.AllValidationTests; +import org.junit.testsupport.AllTestSupportTests; import org.junit.validator.AllValidatorTests; @RunWith(Suite.class) @@ -37,6 +38,7 @@ AllRunnerTests.class, AllRunningTests.class, AllSamplesTests.class, + AllTestSupportTests.class, AllValidationTests.class, AllValidatorTests.class, AssumptionViolatedExceptionTest.class, diff --git a/src/test/java/org/junit/testsupport/AllTestSupportTests.java b/src/test/java/org/junit/testsupport/AllTestSupportTests.java new file mode 100644 index 0000000000000..2663f9bb3442f --- /dev/null +++ b/src/test/java/org/junit/testsupport/AllTestSupportTests.java @@ -0,0 +1,12 @@ +package org.junit.testsupport; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + EventCollectorMatchersTest.class, + EventCollectorTest.class +}) +public class AllTestSupportTests { +} diff --git a/src/test/java/org/junit/testsupport/EventCollectorMatchersTest.java b/src/test/java/org/junit/testsupport/EventCollectorMatchersTest.java new file mode 100644 index 0000000000000..dc7485177f108 --- /dev/null +++ b/src/test/java/org/junit/testsupport/EventCollectorMatchersTest.java @@ -0,0 +1,65 @@ +package org.junit.testsupport; + +import org.hamcrest.Matcher; +import org.junit.Test; +import org.junit.runner.Description; +import org.junit.runner.Result; +import org.junit.runner.RunWith; +import org.junit.runner.notification.Failure; +import org.junit.runners.Parameterized; + +import java.util.Arrays; + +import static java.util.Arrays.asList; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.runners.Parameterized.Parameter; +import static org.junit.runners.Parameterized.Parameters; +import static org.junit.testsupport.EventCollectorMatchers.everyTestRunSuccessful; +import static org.junit.testsupport.EventCollectorMatchers.hasNoAssumptionFailure; +import static org.junit.testsupport.EventCollectorMatchers.hasSingleAssumptionFailure; + +@RunWith(Parameterized.class) +public class EventCollectorMatchersTest { + private static final Description DUMMY_DESCRIPTION = Description.EMPTY; + + private static final Failure DUMMY_FAILURE = new Failure(null, new RuntimeException("dummy message")); + + private static final Result DUMMY_RESULT = new Result(); + + private static final EventCollector COLLECTOR_WITH_NO_EVENTS = new EventCollector(); + + private static final EventCollector COLLECTOR_WITH_SINGLE_FAILURE = new EventCollector() {{ + testFailure(DUMMY_FAILURE); + }}; + + private static final EventCollector COLLECTOR_WITH_SINGLE_ASSUMPTION_FAILURE = new EventCollector() {{ + testAssumptionFailure(DUMMY_FAILURE); + }}; + + @Parameters(name = "{0}") + public static Iterable data() { + return asList( + new Object[] {"everyTestRunSuccessful() matches if no failures are reported", COLLECTOR_WITH_NO_EVENTS, everyTestRunSuccessful()}, + new Object[] {"everyTestRunSuccessful() does not match if failure is reported", COLLECTOR_WITH_SINGLE_FAILURE, not(everyTestRunSuccessful())}, + new Object[] {"everyTestRunSuccessful() does not match if assumption failure is reported", COLLECTOR_WITH_SINGLE_ASSUMPTION_FAILURE, not(everyTestRunSuccessful())}, + new Object[] {"hasNoAssumptionFailure() matches if no assumption failure is reported", COLLECTOR_WITH_NO_EVENTS, hasNoAssumptionFailure()}, + new Object[] {"hasNoAssumptionFailure() does not match if assumption failure is reported", COLLECTOR_WITH_SINGLE_ASSUMPTION_FAILURE, not(hasNoAssumptionFailure())}, + new Object[] {"hasSingleAssumptionFailure() matches if single assumption failure is reported", COLLECTOR_WITH_SINGLE_ASSUMPTION_FAILURE, hasSingleAssumptionFailure()}, + new Object[] {"hasSingleAssumptionFailure() does not match if no assumption failure is reported", COLLECTOR_WITH_NO_EVENTS, not(hasSingleAssumptionFailure())}); + } + + @Parameter(0) + public String testName; //must be assigned. Otherwise the Parameterized runner fails. + + @Parameter(1) + public EventCollector collector; + + @Parameter(2) + public Matcher matcher; + + @Test + public void matchesCollector() { + assertThat(collector, matcher); + } +} diff --git a/src/test/java/org/junit/testsupport/EventCollectorTest.java b/src/test/java/org/junit/testsupport/EventCollectorTest.java new file mode 100644 index 0000000000000..b92d09d077948 --- /dev/null +++ b/src/test/java/org/junit/testsupport/EventCollectorTest.java @@ -0,0 +1,139 @@ +package org.junit.testsupport; + +import static java.util.Collections.singletonList; +import static org.junit.Assert.assertEquals; +import static org.junit.rules.ExpectedException.none; + +import java.util.List; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.Description; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class EventCollectorTest { + private static final Description DUMMY_DESCRIPTION = Description.EMPTY; + private static final Failure DUMMY_FAILURE = new Failure(null, null); + private static final Result DUMMY_RESULT = new Result(); + + @Rule + public final ExpectedException thrown = none(); + + private final EventCollector collector = new EventCollector(); + + @Test + public void collectsTestRunsStarted() { + collector.testRunStarted(DUMMY_DESCRIPTION); + assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestRunsStarted()); + } + + @Test + public void returnsUnmodifiableListOfTestRunsStarted() { + assertNoDescriptionCanBeAddedToList(collector.getTestRunsStarted()); + } + + @Test + public void collectsTestRunsFinished() { + collector.testRunFinished(DUMMY_RESULT); + assertEquals(singletonList(DUMMY_RESULT), collector.getTestRunsFinished()); + } + + @Test + public void returnsUnmodifiableListOfTestRunsFinished() { + assertNoResultCanBeAddedToList(collector.getTestRunsFinished()); + } + + @Test + public void collectsTestSuitesStarted() { + collector.testSuiteStarted(DUMMY_DESCRIPTION); + assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestSuitesStarted()); + } + + @Test + public void returnsUnmodifiableListOfTestSuitesStarted() { + assertNoDescriptionCanBeAddedToList(collector.getTestSuitesStarted()); + } + + @Test + public void collectsTestSuitesFinished() { + collector.testSuiteFinished(DUMMY_DESCRIPTION); + assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestSuitesFinished()); + } + + @Test + public void returnsUnmodifiableListOfTestSuitesFinished() { + assertNoDescriptionCanBeAddedToList(collector.getTestSuitesFinished()); + } + + @Test + public void collectsTestsStarted() { + collector.testStarted(DUMMY_DESCRIPTION); + assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestsStarted()); + } + + @Test + public void returnsUnmodifiableListOfTestsStarted() { + assertNoDescriptionCanBeAddedToList(collector.getTestsStarted()); + } + + @Test + public void collectsTestsFinished() { + collector.testFinished(DUMMY_DESCRIPTION); + assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestsFinished()); + } + + @Test + public void returnsUnmodifiableListOfTestsFinished() { + assertNoDescriptionCanBeAddedToList(collector.getTestsFinished()); + } + + @Test + public void collectsFailures() { + collector.testFailure(DUMMY_FAILURE); + assertEquals(singletonList(DUMMY_FAILURE), collector.getFailures()); + } + + @Test + public void returnsUnmodifiableListOfFailures() { + assertNoFailureCanBeAddedToList(collector.getFailures()); + } + + @Test + public void collectsAssumptionFailures() { + collector.testAssumptionFailure(DUMMY_FAILURE); + assertEquals(singletonList(DUMMY_FAILURE), collector.getAssumptionFailures()); + } + + @Test + public void returnsUnmodifiableListOfAssumptionFailures() { + assertNoFailureCanBeAddedToList(collector.getAssumptionFailures()); + } + + @Test + public void collectsTestsIgnored() { + collector.testIgnored(DUMMY_DESCRIPTION); + assertEquals(singletonList(DUMMY_DESCRIPTION), collector.getTestsIgnored()); + } + + @Test + public void returnsUnmodifiableListOfTestsIgnored() { + assertNoDescriptionCanBeAddedToList(collector.getTestsIgnored()); + } + + private void assertNoDescriptionCanBeAddedToList(List list) { + thrown.expect(Exception.class); + list.add(DUMMY_DESCRIPTION); + } + + private void assertNoFailureCanBeAddedToList(List list) { + thrown.expect(Exception.class); + list.add(DUMMY_FAILURE); + } + + private void assertNoResultCanBeAddedToList(List list) { + thrown.expect(Exception.class); + list.add(DUMMY_RESULT); + } +}