简体   繁体   English

您如何在模块化 Java 项目中组织测试?

[英]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:我正在使用以下版本:

  • junit.jupiter version 5.3.0 (first take was also unsuccessful with version 5.2.0) junit.jupiter 5.3.0 版(第一次尝试也没有成功 5.2.0 版)
  • maven-compiler-plugin version 3.8.0 (first take was also unsuccessful with version 3.7.0) maven-compiler-plugin 版本 3.8.0(第一次尝试在 3.7.0 版本中也不成功)
  • maven-surefire-plugin version 2.22.0 (first take was also unsuccessful with version 2.21.0) maven-surefire-plugin 版本 2.22.0(第一次尝试在 2.21.0 版本中也不成功)

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:在这里,您主要有两种方法可以实现这一点:

  • "compile modular main sources" and "patch plain test sources" at test-runtime with some additional "JVM options hacking the Module system" to execute tests.在测试运行时“编译模块化主要源代码”和“补丁纯测试源代码”,并带有一些额外的“JVM 选项入侵模块系统”来执行测试。
  • "compile modular test sources" and "patch modular main sources" at compile-time to execute tests.在编译时“编译模块化测试源”和“修补模块化主要源”以执行测试。

Blog博客

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

Examples例子

Background and other resources背景和其他资源

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.

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