如何让 Eclipse 检测包与文件夹不匹配的 Scala JUnit 测试?
在 Scala 中,包不一定需要与文件夹匹配。如果不关心工具,我宁愿省略包中如此冗余的组织前缀,以允许更浅的路径。
我的问题是 Eclipse JUnit 插件似乎使用文件夹而不是类路径。当我将 Scala 测试类放在与包匹配的文件夹中时,一切正常。然而,如果我按照我希望的方式放置它们,我会得到 ClassNotFoundException。
假设项目中所有类的包前缀是 org.myorganization.myproduct。然后我想要像这样的文件夹
src/test/scala/domainpackage1/
而不是
src/test/scala/org/myorganisation/myproduct/domainpackage1
但是如果我放置一个如下所示的测试类:
package org.myorganisation.myproduct;
package domainpackage1
...
@RunWith(classOf[JUnitRunner])
class DomainClass1Spec extends FeatureSpec with GivenWhenThen {
...
}
在文件夹中
src/test/scala/domainpackage1
我得到一个
java.lang.ClassNotFoundException: domainpackage1.DomainClass1Spec
所以看起来JUnit插件正在查看源位置并查找与该源位置匹配的类文件而不是查找班级更加稳定。
我的意思是,应该可以从编译单元(.scala)中的当前位置找到输出(.class),对吧?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Eclipse 中的一个已知问题:
https://bugs.eclipse.org/bugs /show_bug.cgi?id=16209
它被标记为
WONTFIX
。但也许你应该对此发表评论并指出 Scala 的问题......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...