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
Fixes gh-121 (ExpectedException handles JUnit exceptions) #323
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
128553f
Fixes gh-121 (ExpectedException handles JUnit exceptions)
stefanbirkner bfd54d6
deleted duplicate EventCollector
stefanbirkner df70c10
the two handle... methods are returning the rule
stefanbirkner a538d25
Apply code review.
stefanbirkner f3ae021
EventCollector always uses TypeSafeMatcher.
stefanbirkner File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/test/java/org/junit/tests/experimental/rules/EventCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package org.junit.tests.experimental.rules; | ||
|
||
import static org.hamcrest.core.IsEqual.equalTo; | ||
import static org.junit.matchers.JUnitMatchers.both; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.hamcrest.Matcher; | ||
import org.junit.internal.matchers.TypeSafeMatcher; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.Result; | ||
import org.junit.runner.notification.Failure; | ||
import org.junit.runner.notification.RunListener; | ||
|
||
class EventCollector extends RunListener { | ||
static Matcher<EventCollector> everyTestRunSuccessful() { | ||
return both(hasNoFailure()).and(hasNoAssumptionFailure()); | ||
} | ||
|
||
private static Matcher<EventCollector> hasNumberOfFailures( | ||
final int numberOfFailures) { | ||
return new TypeSafeMatcher<EventCollector>() { | ||
@Override | ||
public boolean matchesSafely(EventCollector item) { | ||
return item.fFailures.size() == numberOfFailures; | ||
} | ||
|
||
public void describeTo(org.hamcrest.Description description) { | ||
description.appendText("has "); | ||
description.appendValue(numberOfFailures); | ||
description.appendText(" failures"); | ||
} | ||
}; | ||
} | ||
|
||
static Matcher<EventCollector> hasSingleFailure() { | ||
return hasNumberOfFailures(1); | ||
} | ||
|
||
static Matcher<EventCollector> hasNoFailure() { | ||
return hasNumberOfFailures(0); | ||
} | ||
|
||
private static Matcher<EventCollector> hasNumberOfAssumptionFailures( | ||
final int numberOfFailures) { | ||
return new TypeSafeMatcher<EventCollector>() { | ||
@Override | ||
public boolean matchesSafely(EventCollector item) { | ||
return item.fAssumptionFailures.size() == numberOfFailures; | ||
} | ||
|
||
public void describeTo(org.hamcrest.Description description) { | ||
description.appendText("has "); | ||
description.appendValue(numberOfFailures); | ||
description.appendText(" assumption failures"); | ||
} | ||
}; | ||
} | ||
|
||
static Matcher<EventCollector> hasSingleAssumptionFailure() { | ||
return hasNumberOfAssumptionFailures(1); | ||
} | ||
|
||
static Matcher<EventCollector> hasNoAssumptionFailure() { | ||
return hasNumberOfAssumptionFailures(0); | ||
} | ||
|
||
static Matcher<EventCollector> hasSingleFailureWithMessage(String message) { | ||
return hasSingleFailureWithMessage(equalTo(message)); | ||
} | ||
|
||
static Matcher<EventCollector> hasSingleFailureWithMessage( | ||
final Matcher<String> messageMatcher) { | ||
return new TypeSafeMatcher<EventCollector>() { | ||
@Override | ||
public boolean matchesSafely(EventCollector item) { | ||
return hasSingleFailure().matches(item) | ||
&& messageMatcher.matches(item.fFailures.get(0) | ||
.getMessage()); | ||
} | ||
|
||
public void describeTo(org.hamcrest.Description description) { | ||
description.appendText("has single failure with message "); | ||
messageMatcher.describeTo(description); | ||
} | ||
}; | ||
} | ||
|
||
private final List<Description> fTestRunsStarted= new ArrayList<Description>(); | ||
|
||
private final List<Result> fTestRunsFinished= new ArrayList<Result>(); | ||
|
||
private final List<Description> fTestsStarted= new ArrayList<Description>(); | ||
|
||
private final List<Description> fTestsFinished= new ArrayList<Description>(); | ||
|
||
private final List<Failure> fFailures= new ArrayList<Failure>(); | ||
|
||
private final List<Failure> fAssumptionFailures= new ArrayList<Failure>(); | ||
|
||
private final List<Description> fTestsIgnored= new ArrayList<Description>(); | ||
|
||
@Override | ||
public void testRunStarted(Description description) throws Exception { | ||
fTestRunsStarted.add(description); | ||
} | ||
|
||
@Override | ||
public void testRunFinished(Result result) throws Exception { | ||
fTestRunsFinished.add(result); | ||
} | ||
|
||
@Override | ||
public void testStarted(Description description) throws Exception { | ||
fTestsStarted.add(description); | ||
} | ||
|
||
@Override | ||
public void testFinished(Description description) throws Exception { | ||
fTestsFinished.add(description); | ||
} | ||
|
||
@Override | ||
public void testFailure(Failure failure) throws Exception { | ||
fFailures.add(failure); | ||
} | ||
|
||
@Override | ||
public void testAssumptionFailure(Failure failure) { | ||
fAssumptionFailures.add(failure); | ||
} | ||
|
||
@Override | ||
public void testIgnored(Description description) throws Exception { | ||
fTestsIgnored.add(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb= new StringBuilder(); | ||
sb.append(fTestRunsStarted.size()); | ||
sb.append(" test runs started, "); | ||
sb.append(fTestRunsFinished.size()); | ||
sb.append(" test runs finished, "); | ||
sb.append(fTestsStarted.size()); | ||
sb.append(" tests started, "); | ||
sb.append(fTestsFinished.size()); | ||
sb.append(" tests finished, "); | ||
sb.append(fFailures.size()); | ||
sb.append(" failures, "); | ||
sb.append(fAssumptionFailures.size()); | ||
sb.append(" assumption failures, "); | ||
sb.append(fTestsIgnored.size()); | ||
sb.append(" tests ignored"); | ||
|
||
return sb.toString(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move static Matcher factories to the top of the class.