[英]How do you organize tests in a modular Java project?
I am creating a modular build (using module-info.java) on GitHub , but when adding a module-info.java to the modules that I want modular, no tests can be executed...我正在GitHub 上创建模块化构建(使用 module-info.java),但是当将 module-info.java 添加到我想要模块化的模块时,无法执行任何测试...
How can I achieve this?我怎样才能做到这一点?
I am using the following versions:我正在使用以下版本:
A typical error from the failing tests looks like:失败测试的典型错误如下所示:
java.lang.reflect.InaccessibleObjectException: Unable to make com.github.jactor.rises.commons.dto.UserDtoTest() accessible: module jactor.rises.commons does not "opens com.github.jactor.rises.commons.dto" to unnamed module @65e98b1c java.lang.reflect.InaccessibleObjectException:无法使 com.github.jactor.rises.commons.dto.UserDtoTest() 可访问:模块 jactor.rises.commons 不会“打开 com.github.jactor.rises.commons.dto”到未命名的模块@65e98b1c
Welcome to Testing In The Modular World !欢迎来到模块化世界中的测试!
Which kind of tests do you want write?你想写哪种测试?
Extra-module tests : Create a test-only project (no "src/main" directory) and declare a "src/test/java/module-info.java" module descriptor.额外模块测试:创建一个仅测试项目(没有“src/main”目录)并声明一个“src/test/java/module-info.java”模块描述符。
In-module tests : As it was from Day 1 you need to "blend in"/merge/shadow your test classes into your main classes or vice versa.模块内测试:从第一天开始,您需要将您的测试类“混合”/合并/隐藏到您的主类中,反之亦然。 Here you have mainly two ways to achieve this:在这里,您主要有两种方法可以实现这一点:
https://sormuras.github.io/blog/2018-09-11-testing-in-the-modular-world https://sormuras.github.io/blog/2018-09-11-testing-in-the-modular-world
Work-in-progress blueprint https://github.com/sormuras/sandbox/tree/master/sors-modular-testing-blueprint正在进行中的蓝图https://github.com/sormuras/sandbox/tree/master/sors-modular-testing-blueprint
Integration tests starting with "modular-world-" at https://github.com/sormuras/junit-platform-maven-plugin/tree/master/src/it在https://github.com/sormuras/junit-platform-maven-plugin/tree/master/src/it以“modular-world-”开头的集成测试
There is a (new) option in the Maven Surefire plugin called useModulePath
. Maven Surefire 插件中有一个(新)选项,称为useModulePath
。 This option enables to use the traditional Java 8 class path instead of the module path and ignores a module-info.class
from the main classes not turning on the Java module mode, ie all class on the calls path can be used.此选项允许使用传统的 Java 8 类路径而不是模块路径,并忽略来自未打开 Java 模块模式的主类的module-info.class
,即可以使用调用路径上的所有类。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<!-- allow to use unnamed modules -->
<useModulePath>false</useModulePath>
</configuration>
</plugin>
That way the test sources can be kept as there were in Java 8. Only The main classes have to be modularized.这样,测试源可以像在 Java 8 中一样保留。只有主类必须模块化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.