您如何在模块化 Java 项目中组织测试? [英] How do you organize tests in a modular Java project?
问题描述
我正在GitHub,但是在我想要模块化的模块中添加 module-info.java 时,无法执行任何测试...
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...
我怎样才能做到这一点?
How can I achieve this?
我正在使用以下版本:
- junit.jupiter 5.3.0 版(第一次尝试也没有成功 5.2.0 版)
- maven-compiler-plugin 版本 3.8.0(第一次尝试在 3.7.0 版本中也不成功)
- maven-surefire-plugin 2.22.0 版(2.21.0 版的第一次尝试也不成功)
失败测试的典型错误如下:
A typical error from the failing tests looks like:
java.lang.reflect.InaccessibleObjectException:无法制作com.github.jactor.rises.commons.dto.UserDtoTest() 可访问:模块jactor.rises.commons 不会打开com.github.jactor.rises.commons.dto"到未命名模块@65e98b1c
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
推荐答案
欢迎在模块化世界中进行测试!
您想编写哪种类型的测试?
Which kind of tests do you want write?
额外模块测试:创建一个仅测试项目(没有src/main"目录)并声明一个src/test/java/module-info.java";模块描述符.
Extra-module tests: Create a test-only project (no "src/main" directory) and declare a "src/test/java/module-info.java" module descriptor.
模块内测试:从第 1 天开始,您需要混合"/合并/隐藏您的测试类到您的主类中,反之亦然.在这里,您主要有两种方法可以实现这一点:
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:
- 编译模块化主要源代码"和补丁普通测试源"在测试运行时使用一些额外的JVM 选项入侵模块系统";执行测试.
- 编译模块化测试源"和补丁模块主源"在编译时执行测试.
https://sormuras.github.io/blog/2018-09-11-testing-in-the-modular-world
以modular-world-"开头的集成测试在 https://github.com/sormuras/junit-platform-maven-plugin/tree/master/src/it
Integration tests starting with "modular-world-" at https://github.com/sormuras/junit-platform-maven-plugin/tree/master/src/it
- https://github.com/junit-团队/junit5-samples/tree/master/junit5-modular-world
- https://github.com/forax/pro
- https://blog.codefx.org/java/five-command-line-options-to-hack-the-java-9-module-system/
这篇关于您如何在模块化 Java 项目中组织测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!