我如何仅在JUnit测试中打开包并要求依赖于测试范围模块 [英] How do I open packages and require dependencies on test scope modules only for JUnit testing

查看:396
本文介绍了我如何仅在JUnit测试中打开包并要求依赖于测试范围模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将使用classpath从java 10的jar项目迁移到使用java 9拼图模块的java 11. 该项目有JUnit5测试. 测试依赖项由maven在测试范围内提供.

I'm migrating a jar project from java 10 using classpath to java 11 using the java 9 jigsaw modules. There are JUnit5 tests for the project. The test dependencies are provided at test scope by maven. How to make all packages open for testing but not open when the module is used by another project?

jar项目仅为其他项目提供了几个类(例如实用程序项目)(因此不需要主类).

The jar project is just providing a few classes (like a utility project) for other projects (so no main class needed).

该项目在/src/main/java/a/b/c/中有5个软件包. 使用此jar的项目应该可以访问其中的2个. 其他3个仅供内部使用(供可访问者使用). 测试位于/src/test/java/a/b/c/中. 这些测试具有测试范围中提供的依赖项(JUnit,mockito,junt-params),因为这些测试与使用此jar的项目无关.

The project got 5 packages at /src/main/java/a/b/c/. 2 of them should be accessible for projects using this jar. The other 3 are for internal use only (used by the accessible ones). The tests are located at /src/test/java/a/b/c/. These tests have dependencies (JUnit, mockito, junt-params) provided in test scope, since the tests are not relevant for the projects using this jar

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

我在/src/main/java/中提供了 module-info.java :

module moduleName {

requires java.base;

exports a.b.c.package1;
exports a.b.c.package3;
}

所以现在第1包和第3包中的公共类应该可以按预期供其他项目使用(我尚无法验证).

so now the public classes in package 1 and 3 should be accessible to other projects as expected (I have not been able to verify this yet).

现在运行测试会导致java.lang.reflect.InaccessibleObjectException:无法访问a.b.c.package1.collections.SomeTest():模块moduleName不会打开a.b.c.package1"到未命名的模块@ 6a84a97d

Running the Tests now results in java.lang.reflect.InaccessibleObjectException: Unable to make a.b.c.package1.collections.SomeTest() accessible: module moduleName does not "opens a.b.c.package1" to unnamed module @6a84a97d

当我打开软件包(打开)时,一切运行顺利. 但是现在所有软件包都已打开. 我希望仅在测试时才能访问包2、4和5,并且不应打开包1和3进行反射(因此只能导出).

when I open the packages (with opens) everything runs smooth. But now all packages are now opened. I expect package 2, 4 and 5 to be only accessible while testing and 1 and 3 should not be open for reflection (so only exported).

我开始考虑,因为maven告诉我未命名的模块 @ 6a84a97d ,所以这可能是为测试而创建的模块. 这使我尝试在/src/test/java/中添加 module-info.java 进行测试.

I came to think, since maven tells me unnamed module @6a84a97d, this might be a module created for testing. That made me try adding a module-info.java at /src/test/java/ for testing.

module moduleNameTest {
requires moduleName; // the code to test
requires java.base;  // java.base could be transient on moduleName

// test dependencies
requires org.junit.jupiter.api;
requires org.junit.jupiter.params;
}

现在的行家(3.5.4)指出:

now maven (3.5.4) states:

src/test/java/module-info.java:[3,20] module not found: moduleName
src/test/java/module-info.java:[4,31] module not found: org.junit.jupiter.api
src/test/java/module-info.java:[5,31] module not found: org.junit.jupiter.params

技术:

  • java openJDK 11
  • MVN 3.5.4
  • JUnit 5.3.0
  • Surefire插件3.0.0-M3
  • mockito 2.21.0

如前所述,我希望仅在测试期间可以访问包2、4和5,并且测试将在使用maven构建jar上运行.程序包1和程序包3应该导出以用于其他项目,但不能打开以进行反射(因此只能导出而不打开).

As stated earlier I expect package 2, 4 and 5 to be only accessible while testing and the tests to be run on building the jar with maven. Package 1 and 3 should be exported for use in other projects but not open for reflection (so only exported not opened).

如果您需要其他信息,请随时询问.

If you need additional information don't hesitate to ask.

预先感谢

凯文

推荐答案

欢迎来到模块化世界中进行测试",凯文.

"Welcome to Testing In The Modular World", Kevin.

我在此处整理了有关该主题的博客: https://github.com /sormuras/在模块化世界中测试

I compiled a blog about that topic here: https://github.com/sormuras/testing-in-the-modular-world

基本上,当涉及白盒测试时,您需要在测试编译测试运行时调整模块系统,以允许测试框架绕过模块系统的障碍.

Basically, when it comes to white-box testing, you need to tweak the module system either at test compile or test runtime to allow testing frameworks to by-pass the module system barriers.

我想,您在正确的道路上...也许Surefire做错了什么?想要提供 https://github.com/sormuras/junit-platform-maven-plugin 我写了一个镜头?该插件支持开箱即用的黑盒和白盒测试.特别是,当您提供test/java/module-info.java测试模块描述符时,此插件会发光.

I guess, you're on the right track ... maybe Surefire does the wrong thing? Want to give https://github.com/sormuras/junit-platform-maven-plugin I wrote a shot? This plugin supports black- and white-box testing out of the box. Especially, this plugin shines, when you provide a test/java/module-info.java test module descriptor.

请参见此图片",了解如何组织模块化测试 而无需触摸主模块描述符:

See this "picture" for how to organize modular tests without touching the main module descriptor:

src ├── main │ └── java │ ├── foo │ │ ├── PackageFoo.java │ │ └── PublicFoo.java │ └── module-info.java <------------------ module foo { exports foo; } ├── test │ └── java .--- open module foo { │ ├── foo / exports foo; │ │ └── PackageFooTests.java / requires org.junit.jupiter.api; │ └── module-info.[java|test] <----< } └── it \ └── bar °---- --add-reads └── src foo=org.junit.jupiter.api └── test --add-opens └── java foo/foo=org.junit.platform.commons ├── bar │ └── PublicFooTests.java └── module-info.java <------ open module bar { requires foo; requires org.junit.jupiter.api; }

src ├── main │ └── java │ ├── foo │ │ ├── PackageFoo.java │ │ └── PublicFoo.java │ └── module-info.java <------------------ module foo { exports foo; } ├── test │ └── java .--- open module foo { │ ├── foo / exports foo; │ │ └── PackageFooTests.java / requires org.junit.jupiter.api; │ └── module-info.[java|test] <----< } └── it \ └── bar °---- --add-reads └── src foo=org.junit.jupiter.api └── test --add-opens └── java foo/foo=org.junit.platform.commons ├── bar │ └── PublicFooTests.java └── module-info.java <------ open module bar { requires foo; requires org.junit.jupiter.api; }

此模式也应该易于采用.

This pattern should be easy to adopt to your setup as well.

相关问题:如何做您在模块化Java项目中组织测试?

这篇关于我如何仅在JUnit测试中打开包并要求依赖于测试范围模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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