diff --git a/src/test/java/org/junit/tests/AllTests.java b/src/test/java/org/junit/tests/AllTests.java index 00c259b77320..393f00129ba1 100644 --- a/src/test/java/org/junit/tests/AllTests.java +++ b/src/test/java/org/junit/tests/AllTests.java @@ -104,6 +104,8 @@ import org.junit.tests.validation.BadlyFormedClassesTest; import org.junit.tests.validation.FailedConstructionTest; import org.junit.tests.validation.ValidationTest; +import org.junit.testsupport.EventCollectorMatchersTest; +import org.junit.testsupport.EventCollectorTest; import org.junit.validator.PublicClassValidatorTest; // These test files need to be cleaned. See @@ -204,6 +206,8 @@ JUnitCommandLineParseResultTest.class, FilterFactoriesTest.class, CategoryFilterFactoryTest.class, + EventCollectorMatchersTest.class, + EventCollectorTest.class, FrameworkFieldTest.class, FrameworkMethodTest.class, FailOnTimeoutTest.class, diff --git a/src/test/java/org/junit/testsupport/EventCollector.java b/src/test/java/org/junit/testsupport/EventCollector.java index 1f22d81ea667..04a04c69eb29 100644 --- a/src/test/java/org/junit/testsupport/EventCollector.java +++ b/src/test/java/org/junit/testsupport/EventCollector.java @@ -32,27 +32,27 @@ public class EventCollector extends RunListener { private final List testsIgnored = synchronizedList(new ArrayList()); @Override - public void testRunStarted(Description description) throws Exception { + public void testRunStarted(Description description) { testRunsStarted.add(description); } @Override - public void testRunFinished(Result result) throws Exception { + public void testRunFinished(Result result) { testRunsFinished.add(result); } @Override - public void testStarted(Description description) throws Exception { + public void testStarted(Description description) { testsStarted.add(description); } @Override - public void testFinished(Description description) throws Exception { + public void testFinished(Description description) { testsFinished.add(description); } @Override - public void testFailure(Failure failure) throws Exception { + public void testFailure(Failure failure) { failures.add(failure); } @@ -62,7 +62,7 @@ public void testAssumptionFailure(Failure failure) { } @Override - public void testIgnored(Description description) throws Exception { + public void testIgnored(Description description) { testsIgnored.add(description); } 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 000000000000..dc7485177f10 --- /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 000000000000..719aebc915e5 --- /dev/null +++ b/src/test/java/org/junit/testsupport/EventCollectorTest.java @@ -0,0 +1,127 @@ +package org.junit.testsupport; + +import static java.util.Arrays.asList; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +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() throws Exception { + collector.testRunStarted(DUMMY_DESCRIPTION); + assertThat(collector.getTestRunsStarted(), + is(equalTo(asList(DUMMY_DESCRIPTION)))); + } + + @Test + public void returnsUnmodifiableListOfTestRunsStarted() throws Exception { + assertNoDescriptionCanBeAddedToList(collector.getTestRunsStarted()); + } + + @Test + public void collectsTestRunsFinished() throws Exception { + collector.testRunFinished(DUMMY_RESULT); + assertThat(collector.getTestRunsFinished(), + is(equalTo(asList(DUMMY_RESULT)))); + } + + @Test + public void returnsUnmodifiableListOfTestRunsFinished() throws Exception { + assertNoResultCanBeAddedToList(collector.getTestRunsFinished()); + } + + @Test + public void collectsTestsStarted() throws Exception { + collector.testStarted(DUMMY_DESCRIPTION); + assertThat(collector.getTestsStarted(), + is(equalTo(asList(DUMMY_DESCRIPTION)))); + } + + @Test + public void returnsUnmodifiableListOfTestsStarted() throws Exception { + assertNoDescriptionCanBeAddedToList(collector.getTestsStarted()); + } + + @Test + public void collectsTestsFinished() throws Exception { + collector.testFinished(DUMMY_DESCRIPTION); + assertThat(collector.getTestsFinished(), + is(equalTo(asList(DUMMY_DESCRIPTION)))); + } + + @Test + public void returnsUnmodifiableListOfTestsFinished() throws Exception { + assertNoDescriptionCanBeAddedToList(collector.getTestsFinished()); + } + + @Test + public void collectsFailures() throws Exception { + collector.testFailure(DUMMY_FAILURE); + assertThat(collector.getFailures(), is(equalTo(asList(DUMMY_FAILURE)))); + } + + @Test + public void returnsUnmodifiableListOfFailures() throws Exception { + assertNoFailureCanBeAddedToList(collector.getFailures()); + } + + @Test + public void collectsAssumptionFailures() throws Exception { + collector.testAssumptionFailure(DUMMY_FAILURE); + assertThat(collector.getAssumptionFailures(), + is(equalTo(asList(DUMMY_FAILURE)))); + } + + @Test + public void returnsUnmodifiableListOfAssumptionFailures() throws Exception { + assertNoFailureCanBeAddedToList(collector.getAssumptionFailures()); + } + + @Test + public void collectsTestsIgnored() throws Exception { + collector.testIgnored(DUMMY_DESCRIPTION); + assertThat(collector.getTestsIgnored(), + is(equalTo(asList(DUMMY_DESCRIPTION)))); + } + + @Test + public void returnsUnmodifiableListOfTestsIgnored() throws Exception { + 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); + } +}