Bug 537286 - [JUnit] Cannot run junit tests that have a parameter
Summary: [JUnit] Cannot run junit tests that have a parameter
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.9   Edit
Hardware: All All
: P3 normal with 1 vote (vote)
Target Milestone: 4.10 RC1   Edit
Assignee: Roland Grunberg CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-07-23 07:48 EDT by Stephane Epardaud CLA
Modified: 2018-11-29 04:25 EST (History)
7 users (show)

See Also:
noopur_gupta: review+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stephane Epardaud CLA 2018-07-23 07:48:47 EDT
If I use a custom test runner such as Vert.x's VertxUnitRunner (https://vertx.io/docs/vertx-unit/java/#_junit_integration), Eclipse can't start single tests by selecting them. It can only start the whole file. That's because the matcher that selects the single test is broken.

For example:

@RunWith(VertxUnitRunner.class)
public class JUnitTestSuite {
  @Test
  public void testSomething(TestContext context) {
    context.assertFalse(false);
  }
}

In https://github.com/eclipse/eclipse.jdt.ui/blob/master/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/JUnit4TestLoader.java#L34 I can see that testName will be "testSomething(TestContext)" instead of "testSomething(JUnitTestSuite)". Something is putting the parameter name in parenthesis instead of the test class name.

Later, that testName is extracted in https://github.com/eclipse/eclipse.jdt.ui/blob/master/org.eclipse.jdt.junit4.runtime/src/org/eclipse/jdt/internal/junit4/runner/DescriptionMatcher.java#L134 to represent the test class name, and used to filter the test we're looking for. Which does not work because it will incorrectly look for a test belonging to the class "TestContext" and never find it.

Apparently Eclipse uses "testName(parameterType)" where JUnit uses "testName(testClass)" and that only works for tests that don't take any parameter.
Comment 1 Dani Megert CLA 2018-07-23 08:55:55 EDT
Which Eclipse version and which JUnit launcher did you use?
Comment 2 Stephane Epardaud CLA 2018-07-23 09:07:02 EDT
Oxygen.2 Release (4.7.2) with JUnit 4.12
Comment 3 Noopur Gupta CLA 2018-07-24 06:23:19 EDT
Can you please try with Eclipse Oxygen 4.7 release build and Eclipse Photon 4.8  release build as well? This will help us in figuring out if the issue is due to any regression in 4.7.2 and if it is something that's already fixed in 4.8.
Comment 4 Stephane Epardaud CLA 2018-07-25 10:58:13 EDT
The issue is still present in Photon.
Comment 5 Roland Grunberg CLA 2018-08-02 13:55:54 EDT
Build id: I20180801-2000
Reproduced against eclipse.jdt.ui master branch.

It looks like when the test element selected is an IMethod, in org.eclipse.jdt.junit.core/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationDelegate.java#collectExecutionArguments(..), the -test argument gets passed method.getDeclaringType().getFullyQualifiedName() + ':' + method.getElementName() . The 'method.getElementName()' is where 'testName(parameterType)' originates.

The IMethod is created manually in evaluateTests(..) from the value of 'org.eclipse.jdt.junit.TESTNAME'. This seems to be set by the Junit Launch Tab, and is the displayed format of the method to be run. So simply taking out the the type parameter(s) (if they exist) in evaluateTests should resolve this.

I can upload a rough change once I can confirm all tests pass.
Comment 6 Eclipse Genie CLA 2018-08-02 14:30:25 EDT
New Gerrit change created: https://git.eclipse.org/r/126994
Comment 7 Roland Grunberg CLA 2018-08-03 16:16:37 EDT
In the interest of understanding the issue better, I believe this regressed in Oxygen when type parameter data was introduced in various places (eg. JUnitLaunchConfigurationTab). It ultimately ended up stored in org.eclipse.jdt.junit.core.TESTNAME which was later used as a filter for which test to run.

http://git.eclipse.org/c/gerrit/jdt/eclipse.jdt.ui.git/diff/org.eclipse.jdt.junit/src/org/eclipse/jdt/junit/launcher/JUnitLaunchConfigurationTab.java?id=815287b20b0e86d27692b953e6e275b977f8c2b5
Comment 8 Noopur Gupta CLA 2018-08-13 07:01:09 EDT
See comments on Gerrit.
Comment 9 Sarika Sinha CLA 2018-08-23 00:33:12 EDT
Moving to 4.10.

Not very safe to release the change at this point.
Comment 10 Brook Beam CLA 2018-09-19 16:49:10 EDT
Found my way here as a result of tracing this exact issue through the JDT libraries when attempting to run a single JUnit 4 method using Jukito https://github.com/ArcBees/Jukito which allows for injection and multi-instance bindings of method parameters. I can confirm that the issue seems to begin with an incorrect pattern match for "className" in DescriptionMatcher when running a single method with parameters. We have been experiencing this since Oxygen (and now in Photon). Many of our automated test suites are built on JUnit 4 framework(s) that support test parameterization with a custom runner, and this has been a fairly big quality-of-life degradation. Workaround has been to first run the entire class, and then select the individual method from the JUnit UI and run it (but this requires initialization of the entire class first). Is there indeed no way to include this in 4.9 release? If not, what is the reasoning? I can't seem to find the release schedule for 4.10 yet, any info on when that is?
Comment 12 Noopur Gupta CLA 2018-11-23 02:35:27 EST
(In reply to Eclipse Genie from comment #11)
> Gerrit change https://git.eclipse.org/r/126994 was merged to [master].
> Commit:
> http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=3207b1338106d8d6df839b3ddf5a264fd7e4bd15
Thanks, Roland. I have opened bug 541485 for the issue in navigating to the source.
Comment 13 Noopur Gupta CLA 2018-11-29 04:25:48 EST
Verified as fixed in I20181128-1800.