-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Handling of StoppedByUserException will correctly stop execution in case of any additional errors coming from @AfterClass methods or class rules. #1131
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
import org.junit.runner.notification.StoppedByUserException; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.InitializationError; | ||
import org.junit.runners.model.MultipleFailureException; | ||
import org.junit.runners.model.RunnerScheduler; | ||
import org.junit.runners.model.Statement; | ||
import org.junit.runners.model.TestClass; | ||
|
@@ -363,13 +364,60 @@ public void run(final RunNotifier notifier) { | |
statement.evaluate(); | ||
} catch (AssumptionViolatedException e) { | ||
testNotifier.addFailedAssumption(e); | ||
} catch (StoppedByUserException e) { | ||
throw e; | ||
} catch (Throwable e) { | ||
testNotifier.addFailure(e); | ||
processExecutionFailures(testNotifier, gatherAllFailures(e)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a simpler suggestion that still works. We could add a method to Note that in #1118 someone is proposing adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, this was one of my first attempt to fix this issue, but returning to my points in the previous conversation - if we will simply pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikitamerinov So is there a problem with my suggestion to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kcooney Partially. Your suggestion will prevent other classes from execution, but we still need to do something with notifying clients about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikitamerinov if we don't want to notify users about that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As it was said, updating But, in this case, we can distinguish @kcooney Are we ok with that approach? |
||
} | ||
|
||
/** | ||
* Goes through all execution failures trying to find and re-throw | ||
* {@link StoppedByUserException} while adding all other errors to | ||
* test notifier. | ||
* | ||
* @param testNotifier notifier to add failures | ||
* @param failures list of execution failures | ||
*/ | ||
private void processExecutionFailures(EachTestNotifier testNotifier, | ||
List<Throwable> failures) { | ||
StoppedByUserException stoppedByUserException = null; | ||
|
||
for (Throwable failure : failures) { | ||
if (failure instanceof StoppedByUserException) { | ||
stoppedByUserException = (StoppedByUserException) failure; | ||
} else { | ||
testNotifier.addFailure(failure); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a behavior change. You are sending to the notifier each of the failures that was contained in the It sounds the problem is that a Also, where is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Behavior change from what point of view? Before and after my changes no clients are getting multiple exceptions at once. As I said in item 2 in my pull request message, class
Sending
You can get full example from #1125 or check new test I've added. Basically, |
||
} | ||
} | ||
|
||
if (stoppedByUserException != null) { | ||
throw stoppedByUserException; | ||
} | ||
} | ||
|
||
/** | ||
* Returns all failures from the specified exception going through each | ||
* encountered {@link MultipleFailureException} recursively. | ||
* | ||
* @param throwable input exception | ||
* @return list of all failures | ||
*/ | ||
private List<Throwable> gatherAllFailures(Throwable throwable) { | ||
List<Throwable> result = new ArrayList<Throwable>(); | ||
|
||
if (throwable instanceof MultipleFailureException) { | ||
MultipleFailureException multipleFailureException = | ||
(MultipleFailureException) throwable; | ||
|
||
for (Throwable failure : multipleFailureException.getFailures()) { | ||
result.addAll(gatherAllFailures(failure)); | ||
} | ||
} else { | ||
result.add(throwable); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// | ||
// Implementation of Filterable and Sortable | ||
// | ||
|
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.
Why was the catch block for
StoppedByUserException
removed?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.
Now
StoppedByUserException
is handled in theprocessExecutionFailures()
method, despite single or multiple exceptions came from evaluating class block statement.