简体   繁体   English

JUnit在Eclipse上测试Java 9模块化项目

[英]JUnit tests in Eclipse on Java 9 modular project

I'd like to run tests with JUnit 5 on Java 9 modular project in Eclipse, with no Maven, Gradle or all that fancy stuff. 我想在Eclipse中使用JUnit 5在Java 9模块化项目上运行测试,没有Maven,Gradle或所有那些花哨的东西。 So I have src/main/java path where module-info.java and module's packages live and also src/test/java where all the test classes are. 所以我有src/main/java路径,其中module-info.java和模块的包存在, src/test/java也是所有测试类。 Id est business as usual, prior to the Jigsaw module system. 在Jigsaw模块系统之前,照常进行业务。 I have Eclipse Oxygen.3a (4.7.3a) an Java 10.0.1. 我有Eclipse Oxygen.3a(4.7.3a)和Java 10.0.1。

I've seen some video from Eclipse showing, how to add JUnit test to modular project, but this flabbergasted me deeply: they put required keyword into module-info.java of a module, binding it to JUnit module. 我已经看到Eclipse中的一些视频显示了如何将JUnit测试添加到模块化项目中,但这让我大吃一惊:他们将required关键字放入模块的module-info.java中,并将其绑定到JUnit模块。 Is that actually even correct? 这实际上是否正确?

I've seen also all these --patch-module / --add-reads solutions (when we're talking about working in a console) and it seems like it's the proper way to do it, but I have no idea, how to achieve that in Eclipse without binding module under test to JUnit module. 我也看到了所有这些--patch-module / --add-reads解决方案(当我们谈论在控制台中工作时)它似乎是正确的方法,但我不知道,如何在没有绑定模块的情况下在Eclipse中实现,以便对JUnit模块进行测试。 Is that even possible in Eclipse (without Maven and so)? 这在Eclipse中是否可行(没有Maven等)?

I tried to solve this problem for quite a while, too. 我也尝试解决这个问题很长一段时间了。 My approach is to add a filter to the source code directory for src/main/java that filters out the module-info.java. 我的方法是在src / main / java的源代码目录中添加一个过滤器,过滤掉module-info.java。 This allows to have a different module-info.java in src/test/java. 这允许在src / test / java中使用不同的module-info.java。 It will be this one that gets copied to the output folder. 这将被复制到输出文件夹。 This way you can run your unit tests from within the IDE and use the other one for the final build. 这样,您可以在IDE中运行单元测试,并使用另一个进行最终构建。 However, you need to keep the content of the one in src/main/java updated yourself. 但是,您需要自己更新src / main / java中的内容。

Right click on the project > Properties > Java Build Path > Source 右键单击项目> Properties> Java Build Path> Source

Select the src/main/java entry, click Edit > Next > Exclusion Patterns > Add 选择src / main / java条目,单击编辑>下一步>排除模式>添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM