How can I make Eclipse detect Scala JUnit tests wh

2019-04-16 04:53发布

问题:

In Scala, packages doesn't necessarily need to match folders. And if tooling wasn't concerned I would rather leave out the ever so redundant organisation prefix of the package to allow for shallower paths.

My problem is that the Eclipse JUnit plugin seems to be working with folders rather than classpath. When I place my Scala test classes in folders matching the package everything works fine. If I however put them the way I would like I get a ClassNotFoundException.

Say my package prefix is org.myorganisation.myproduct for all classes in a project. Then I would like to have folders like

src/test/scala/domainpackage1/

instead of

src/test/scala/org/myorganisation/myproduct/domainpackage1

but if I put a test class looking like:

package org.myorganisation.myproduct;
package domainpackage1
...
@RunWith(classOf[JUnitRunner])
class DomainClass1Spec extends FeatureSpec with GivenWhenThen {
...
}

in the folder

src/test/scala/domainpackage1

I get a

java.lang.ClassNotFoundException: domainpackage1.DomainClass1Spec

So it seems the JUnit plugin is looking at the source location and looks for a class file matching that rather than finding the class in a more stable way.

I mean, it should be possible to find the output (.class) from the current position in the compilation unit (.scala), right?

回答1:

This is a known issue in Eclipse:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=16209

It's marked as WONTFIX. But perhaps you should comment on it and point out the issue for Scala...