Skip to content

Commit

Permalink
Add test for handling InitializationError
Browse files Browse the repository at this point in the history
The code was changed by commit 0804ef4
but unfortunately the test was not part of the commit. The test is based
on Philip Graf's test in pull request junit-team#1065.

Co-authored-by: Philip Graf <[email protected]>
  • Loading branch information
stefanbirkner and acanda committed Feb 17, 2018
1 parent 627b85a commit 3d7de19
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/test/java/org/junit/rules/EventCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

class EventCollector extends RunListener {
public class EventCollector extends RunListener {
static Matcher<EventCollector> everyTestRunSuccessful() {
return allOf(hasNoFailure(), hasNoAssumptionFailure());
}
Expand Down Expand Up @@ -73,7 +73,7 @@ static Matcher<EventCollector> hasNoAssumptionFailure() {
return hasNumberOfAssumptionFailures(0);
}

static Matcher<EventCollector> hasSingleFailureWithMessage(String message) {
public static Matcher<EventCollector> hasSingleFailureWithMessage(String message) {
return hasSingleFailureWithMessage(equalTo(message));
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/junit/runner/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.rules.EventCollector.hasSingleFailureWithMessage;

import org.junit.Test;
import org.junit.rules.EventCollector;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;

public class RequestTest {

Expand All @@ -18,4 +22,29 @@ public void createsADescriptionWithANameForClasses() {
.getDescription();
assertThat(description.toString(), is("classes"));
}

@Test
public void reportsInitializationErrorThrownWhileCreatingSuite() {
EventCollector collector = new EventCollector();
JUnitCore core = new JUnitCore();
core.addListener(collector);

core.run(new FailingComputer(), FooTest.class, BarTest.class);

assertThat(collector, hasSingleFailureWithMessage("cannot create suite"));
}

private static class FailingComputer extends Computer {
@Override
public Runner getSuite(RunnerBuilder builder, Class<?>[] classes)
throws InitializationError {
throw new InitializationError("cannot create suite");
}
}

private static class FooTest {
}

private static class BarTest {
}
}

0 comments on commit 3d7de19

Please sign in to comment.