Skip to content
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

Fix swallowed exception in Request.classes(...) #1065

Closed
wants to merge 2 commits into from

Conversation

acanda
Copy link
Contributor

@acanda acanda commented Jan 6, 2015

The method classes(Computer computer, Class<?>... classes) in
org.junit.runner.Request swallows an InitializationError and throws a
new RuntimeException instead when it cannot create a request. This
prevents the user from finding out why the request could not be created.

This change replaces throwing a new RuntimeException with generating a
proper error report. The report contains the causes of the
InitializationError so the user has a chance to find out what is causing
the problem.

Instead of an incomplete stack trace with the message "Bug in saff's brain: Suite constructor, called as above, should always complete" like

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    <snip>
Caused by: java.lang.RuntimeException: Bug in saff's brain: Suite constructor, called as above, should always complete
    at org.junit.runner.Request.classes(Request.java:72)
    at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.createRequestAndRun(JUnitCoreWrapper.java:102)
    at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.executeEager(JUnitCoreWrapper.java:85)
    at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:54)
    ... 27 more

the user gets an error report like

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running ch.acanda.eclipse.pmd.unsupported.ProjectPropertyTesterTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.002 sec <<< FAILURE! - in ch.acanda.eclipse.pmd.unsupported.ProjectPropertyTesterTest
initializationError(ch.acanda.eclipse.pmd.unsupported.ProjectPropertyTesterTest)  Time elapsed: 0.002 sec  <<< ERROR!
org.apache.maven.surefire.testset.TestSetFailedException: Unspecified thread-count(s). See the parameters useUnlimitedThreads, threadcount, threadcountsuites, threadcountclasses, threadcountmethods.
    at org.apache.maven.surefire.junitcore.pc.ParallelComputerUtil.resolveConcurrency(ParallelComputerUtil.java:69)
    at org.apache.maven.surefire.junitcore.pc.ParallelComputerBuilder$PC.determineThreadCounts(ParallelComputerBuilder.java:312)
    at org.apache.maven.surefire.junitcore.pc.ParallelComputerBuilder$PC.getSuite(ParallelComputerBuilder.java:272)
    at org.junit.runner.Request.classes(Request.java:75)
    <snip>
    at org.eclipse.equinox.launcher.Main.main(Main.java:1386)


Results :

Tests in error: 
  Unspecified thread-count(s). See the parameters useUnlimitedThreads, threadcount, threadcountsuites, threadcountclasses, threadcountmethods.

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

The method classes(Computer computer, Class<?>... classes) in
org.junit.runner.Request swallows an InitializationError and throws a
new RuntimeException instead when it cannot create a request. This
prevents the user from finding out why the request could not be created.

This change replaces throwing a new RuntimeException with generating a
proper error report. The report contains the causes of the
InitializationError so the user has a chance to find out what is causing
the problem.
@acanda
Copy link
Contributor Author

acanda commented Jan 6, 2015

PS: if you came here because you just updated Tycho to version 0.22, your tests don't run any more and you googled java.lang.RuntimeException: Bug in saff's brain: Suite constructor, called as above, should always complete because this was the only hint you have, here's the solution to your problem: you are executing your tests in parallel but you did not specify the number of threads JUnit should use. You need to set one of the parameters useUnlimitedThreads, threadcount, threadcountsuites, threadcountclasses, threadcountmethods in the configuration of your tycho-surefire-plugin, e.g.

            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-surefire-plugin</artifactId>
                <version>${tycho-version}</version>
                <configuration>
                    <parallel>classes</parallel>
                    <perCoreThreadCount>true</perCoreThreadCount>
                    <threadCount>1</threadCount>
                 </configuration>
            </plugin>

@kcooney
Copy link
Member

kcooney commented Jan 6, 2015

Could you include in this pull a test that calls Request.classes() that triggers this exception? It sounds like David Saff didn't expect us to get to this throw statement

@acanda
Copy link
Contributor Author

acanda commented Jan 6, 2015

I added a test that triggers the exception when calling Request.classes(...) and asserts that the run listener is notified with a failure instead of throwing a RuntimeException.

@stefanbirkner
Copy link
Contributor

LGTM

@Override
public Description getDescription() {
Description description = Description.createSuiteDescription(testClass);
Description description = Description.createSuiteDescription(classNames);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an externally visible change for the case where the existing constructor is used.

Instead of making suggestions here, I'm going to create a pull with your commits and one additional commit to fix this and to fix some other minor issues.

The other pull is just to make reviewing my proposed changes easier; I still plan on merging your pull. Details to follow soon.

@kcooney
Copy link
Member

kcooney commented Jan 19, 2015

I patched your branch into my repository, added a match to fix the backwards compatibility issue. You can view it at #1072

Note there isn't much tests for the changes to ErrorReportingRunner; I'll add them if you guys think this is the right direction to take.

It's a bit more complicated than I would like. I'm tempted to say that we should not change ErrorReportingRunner at all, and have Request.classes() pass in Request.class as the class.

If you like the direction, but want to make some changes, feel free to pull from my branch back to yours. To make reviewing simpler, I suggest not squashing commits for now.

@kcooney
Copy link
Member

kcooney commented Jan 25, 2015

@acanda can you comment on pull #1072 please?

@acanda
Copy link
Contributor Author

acanda commented Jan 27, 2015

@kcooney your changes look similar to something I also had but though to be too complicated ;-)
I just looked at your changes on github as I am a bit busy at the moment. I have more time later this week. Maybe we find a less complicated way without the regression issue.
Generally though I'm happy as long as the exception is reported somewhere.

@kcooney
Copy link
Member

kcooney commented Jan 27, 2015

@acanda as I stated before, I wasn't excited about the complexity of my changes either. That's why I was wondering whether we really need to include all of the classes passed to Request.classes() in the top-level Description for the case we get an exception trying to create a runner.

stefanbirkner added a commit to stefanbirkner/junit that referenced this pull request Feb 15, 2018
The code was changed by commit 0804ef4
but unfortunately the test was not part of the commit. I recovered the
test from Philip Graf's original pull request junit-team#1065.

Co-authored-by: Philip Graf <[email protected]>
stefanbirkner added a commit to stefanbirkner/junit that referenced this pull request Feb 15, 2018
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]>
stefanbirkner added a commit to stefanbirkner/junit that referenced this pull request Feb 15, 2018
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]>
@stefanbirkner
Copy link
Contributor

This pull request has been continued in #1072.

stefanbirkner added a commit that referenced this pull request Feb 17, 2018
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 #1065.

Co-authored-by: Philip Graf <[email protected]>
sebasjm pushed a commit to sebasjm/junit4 that referenced this pull request Mar 11, 2018
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]>
aristotle0x01 pushed a commit to aristotle0x01/junit4 that referenced this pull request Jun 27, 2022
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants