Java 9 + maven + junit:测试代码是否需要自己的module-info.java以及放置它的位置? [英] Java 9 + maven + junit: does test code need module-info.java of its own and where to put it?

查看:379
本文介绍了Java 9 + maven + junit:测试代码是否需要自己的module-info.java以及放置它的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个使用Maven 3和junit的Java项目。有$ code> src / main / java 和 src / test / java 目录分别包含主要来源和测试来源(一切都是标准的。)

Let's say I have a Java project using Maven 3 and junit. There are src/main/java and src/test/java directories which contain main sources and test sources, respectively (everything is standard).

现在我想将项目迁移到Java 9. src / main / java 内容代表Java 9模块;有 com / acme / project / module-info.java 看起来大概是这样的:

Now I want to migrate the project to Java 9. src/main/java content represents Java 9 module; there is com/acme/project/module-info.java looking approximately like this:

module com.acme.project {
    require module1;
    require module2;
    ...
}

如果测试代码需要 module-info.java 属于自己的吗?例如,添加对某些模块的依赖性,该模块仅用于测试,而不是生产代码。在这种情况下,我必须将 module-info.java 放到 src / test / java / com / acme / project / 给模块一个不同的名称。这样Maven似乎将主要源和测试源视为不同的模块,因此我必须将包从主模块导出到测试模块,并且需要测试模块中的包,如下所示:

What if test code needs module-info.java of its own? For example, to add a dependence on some module that is only needed for tests, not for production code. In such a case, I have to put module-info.java to src/test/java/com/acme/project/ giving the module a different name. This way Maven seems to treat main sources and test sources as different modules, so I have to export packages from the main module to the test module, and require packages in the test module, something like this:

主模块(在 src / main / java / com / acme / project 中):

module prod.module {
    exports com.acme.project to test.module;
}

测试模块(在 src / test / java /中com / acme / project ):

module test.module {
    requires junit;
    requires prod.module;
}

这会产生

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project test-java9-modules-junit: Compilation failure: Compilation failure:
[ERROR] /home/rpuch/git/my/test-java9-modules-junit/src/test/java/com/acme/project/GreeterTest.java:[1,1] package exists in another module: prod.module

因为一个包在两个模块中定义。所以现在我必须在主模块和测试模块中有不同的项目,这是不方便的。

because one package is defined in two modules. So now I have to have different projects in main module and test module, which is not convenient.

我觉得我走错路,一切都开始看起来很难看。如何在测试代码中拥有自己的 module-info.java ,或者如何实现相同的效果( require 等没有它?

I feel I follow wrong path, it all starts looking very ugly. How can I have module-info.java of its own in test code, or how do I achieve the same effects (require, etc) without it?

推荐答案

模块系统不区分生产代码和测试代码,所以如果你选择模块化测试代码, prod.module test.module 不能共享相同的包 com.acme.project ,如 specs <中所述/ a>:

The module system does not distinguish between production code and test code, so if you choose to modularize test code, the prod.module and the test.module cannot share the same package com.acme.project, as described in the specs:


无干扰 - Java编译器,虚拟机和运行时系统必须确保包含相同名称包的模块不会相互干扰。如果两个不同的模块包含相同名称的包,那么从每个模块的角度来看,该包中的所有类型和成员仅由该模块定义。一个模块中该包中的代码必须无法访问另一个模块中该包中的包私有类型或成员。

Non-interference — The Java compiler, virtual machine, and run-time system must ensure that modules that contain packages of the same name do not interfere with each other. If two distinct modules contain packages of the same name then, from the perspective of each module, all of the types and members in that package are defined only by that module. Code in that package in one module must not be able to access package-private types or members in that package in the other module.

如Alan Bateman所示,Maven编译器插件使用 --patch-module和模块系统在编译src / test / java树中的代码时提供的其他选项,以便使用测试类扩充测试中的模块。在运行测试类时,这也是由Surefire插件完成的(请参阅支持运行单元测试命名为Java 9模块)。这意味着您无需将测试代码放在模块中。

As indicated by Alan Bateman, the Maven compiler plugin uses --patch-module and other options provided by the module system when compiling code in the src/test/java tree, so that the module under test is augmented with the test classes. And this is also done by the Surefire plugin when running the test classes (see Support running unit tests in named Java 9 modules). This means you don't need to place your test code in a module.

这篇关于Java 9 + maven + junit:测试代码是否需要自己的module-info.java以及放置它的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆