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?

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

问题描述

假设我有一个使用 Maven 3 和 junit 的 Java 项目.src/main/javasrc/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好像把main source和test source当成不同的模块,所以我得把包从main模块导出到test模块,在test模块里require包,是这样的:

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,如 规格:

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 插件完成的(参见 Support running unit tests in命名的 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天全站免登陆