如何让 JUnit5 与 JDK10(拼图)和 Maven3 一起工作?
How to get JUnit5 with JDK10 (jigsaw) and Maven3 to work?
我和一些朋友一起开发了一个 jdk 10 应用程序,它使用拼图功能集(在 jdk9 中引入)和 junit5 以及 maven 作为构建管理。
但是如果我们尝试 运行 使用 Maven 进行测试,我们总是会得到一个 InaccessibleObjectException
异常如:
[ERROR] resourceSchedule Time elapsed: 0.001 s <<< ERROR!
java.lang.reflect.InaccessibleObjectException: Unable to make de.truncated.framework.shared.resources.ResourceManagerTest() accessible: module de.truncated.framework.shared does not "opens de.truncated.framework.shared.resources" to unnamed module @ed7f8b4
我几天以来一直在谷歌搜索...在 jdk9 中可以简单地用 --permit-illegal-access 来解决,但这不可能在 jdk10 中,因为此标志已被删除。 jdk10 个代码示例来自 junit5 和 github 上的其他人,但看起来并没有使用拼图。
看来我错过了 maven、模块或 jvm args 配置中的一些重要内容。
如果有人能在这件事上提供帮助,那就太好了。
非常感谢!
项目的一些可能有用的附加信息:
- 主要代码由一个模块信息文件配置,该文件导出(不打开)给所有人(它是一个类似于代码库的框架)
- 测试代码没有模块化
- 当前没有应用任何 jvm args
使用的版本:
<properties>
<java.version>10</java.version>
<maven.compiler.version>3.7.0</maven.compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire.version>2.21.0</surefire.version>
<asm.version>6.1.1</asm.version>
<junit.jupiter.version>5.2.0</junit.jupiter.version>
<junit.platform.version>1.2.0</junit.platform.version>
</properties>
依赖项:
<!--
junit 5 dependency
-->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
构建插件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
完整的堆栈跟踪:
[INFO] Running de.truncated.framework.shared.resources.ResourceManagerTest
[ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0 s <<< FAILURE! - in de.truncated.framework.shared.resources.ResourceManagerTest
[ERROR] singleThreadedResource Time elapsed: 0 s <<< ERROR!
java.lang.reflect.InaccessibleObjectException: Unable to make de.truncated.framework.shared.resources.ResourceManagerTest() accessible: module de.truncated.framework.shared does not "opens de.truncated.framework.shared.resources" to unnamed module @3af9c5b7
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
at java.base/java.lang.reflect.Constructor.checkCanSetAccessible(Constructor.java:192)
at java.base/java.lang.reflect.Constructor.setAccessible(Constructor.java:185)
at org.junit.platform.commons.util.ReflectionUtils.makeAccessible(ReflectionUtils.java:1332)
at org.junit.platform.commons.util.ReflectionUtils.newInstance(ReflectionUtils.java:429)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:60)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateTestClass(ClassTestDescriptor.java:208)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateAndPostProcessTestInstance(ClassTestDescriptor.java:195)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider[=14=](ClassTestDescriptor.java:185)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider(ClassTestDescriptor.java:189)
at java.base/java.util.Optional.orElseGet(Optional.java:358)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider(ClassTestDescriptor.java:188)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:81)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:58)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.prepare(HierarchicalTestExecutor.java:89)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:74)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively(HierarchicalTestExecutor.java:121)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:177)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively(HierarchicalTestExecutor.java:121)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:177)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:55)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:132)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:111)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:379)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:340)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:413)
今天我找到了两个解决该问题的方法。
1) 你可以添加 --add-opens 到 surefire 插件配置:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<argLine>--add-opens de.truncated.framework.shared/de.truncated.framework.shared.resources=ALL-UNNAMED</argLine>
</configuration>
</plugin>
这会起作用,因为默认情况下 surefire 插件会添加 --add-reads 到 surefireargs 文件,而 JUnit5 使用反射 "reads" 是不够的。
2) 或者,您可以将所有 JUnit5 测试 类(及其注释方法)标记为 public。这也会有所帮助,因为在这种情况下,JUnit 在 运行 测试时不会使用反射(好吧,至少在 5.3.1 版本中不会)。
P.S。我使用了 maven-compiler-plugin 版本 3.8.0 和 maven-surefire-plugin 3.0.0-M1.
Maven Surefire 插件中有一个名为 useModulePath
的(新)选项。此选项允许使用传统的 Java 8 class 路径而不是模块路径并忽略 module-info.class
未打开 Java 模块模式,即所有 [=17=可以使用 class 路径上的 ]es。
<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>
我和一些朋友一起开发了一个 jdk 10 应用程序,它使用拼图功能集(在 jdk9 中引入)和 junit5 以及 maven 作为构建管理。
但是如果我们尝试 运行 使用 Maven 进行测试,我们总是会得到一个 InaccessibleObjectException 异常如:
[ERROR] resourceSchedule Time elapsed: 0.001 s <<< ERROR!
java.lang.reflect.InaccessibleObjectException: Unable to make de.truncated.framework.shared.resources.ResourceManagerTest() accessible: module de.truncated.framework.shared does not "opens de.truncated.framework.shared.resources" to unnamed module @ed7f8b4
我几天以来一直在谷歌搜索...在 jdk9 中可以简单地用 --permit-illegal-access 来解决,但这不可能在 jdk10 中,因为此标志已被删除。 jdk10 个代码示例来自 junit5 和 github 上的其他人,但看起来并没有使用拼图。
看来我错过了 maven、模块或 jvm args 配置中的一些重要内容。 如果有人能在这件事上提供帮助,那就太好了。
非常感谢!
项目的一些可能有用的附加信息:
- 主要代码由一个模块信息文件配置,该文件导出(不打开)给所有人(它是一个类似于代码库的框架)
- 测试代码没有模块化
- 当前没有应用任何 jvm args
使用的版本:
<properties>
<java.version>10</java.version>
<maven.compiler.version>3.7.0</maven.compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire.version>2.21.0</surefire.version>
<asm.version>6.1.1</asm.version>
<junit.jupiter.version>5.2.0</junit.jupiter.version>
<junit.platform.version>1.2.0</junit.platform.version>
</properties>
依赖项:
<!--
junit 5 dependency
-->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
构建插件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
完整的堆栈跟踪:
[INFO] Running de.truncated.framework.shared.resources.ResourceManagerTest
[ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0 s <<< FAILURE! - in de.truncated.framework.shared.resources.ResourceManagerTest
[ERROR] singleThreadedResource Time elapsed: 0 s <<< ERROR!
java.lang.reflect.InaccessibleObjectException: Unable to make de.truncated.framework.shared.resources.ResourceManagerTest() accessible: module de.truncated.framework.shared does not "opens de.truncated.framework.shared.resources" to unnamed module @3af9c5b7
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
at java.base/java.lang.reflect.Constructor.checkCanSetAccessible(Constructor.java:192)
at java.base/java.lang.reflect.Constructor.setAccessible(Constructor.java:185)
at org.junit.platform.commons.util.ReflectionUtils.makeAccessible(ReflectionUtils.java:1332)
at org.junit.platform.commons.util.ReflectionUtils.newInstance(ReflectionUtils.java:429)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:60)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateTestClass(ClassTestDescriptor.java:208)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateAndPostProcessTestInstance(ClassTestDescriptor.java:195)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider[=14=](ClassTestDescriptor.java:185)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider(ClassTestDescriptor.java:189)
at java.base/java.util.Optional.orElseGet(Optional.java:358)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider(ClassTestDescriptor.java:188)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:81)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:58)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.prepare(HierarchicalTestExecutor.java:89)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:74)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively(HierarchicalTestExecutor.java:121)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:177)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively(HierarchicalTestExecutor.java:121)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:177)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:55)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:132)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:111)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:379)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:340)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:413)
今天我找到了两个解决该问题的方法。
1) 你可以添加 --add-opens 到 surefire 插件配置:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<argLine>--add-opens de.truncated.framework.shared/de.truncated.framework.shared.resources=ALL-UNNAMED</argLine>
</configuration>
</plugin>
这会起作用,因为默认情况下 surefire 插件会添加 --add-reads 到 surefireargs 文件,而 JUnit5 使用反射 "reads" 是不够的。
2) 或者,您可以将所有 JUnit5 测试 类(及其注释方法)标记为 public。这也会有所帮助,因为在这种情况下,JUnit 在 运行 测试时不会使用反射(好吧,至少在 5.3.1 版本中不会)。
P.S。我使用了 maven-compiler-plugin 版本 3.8.0 和 maven-surefire-plugin 3.0.0-M1.
Maven Surefire 插件中有一个名为 useModulePath
的(新)选项。此选项允许使用传统的 Java 8 class 路径而不是模块路径并忽略 module-info.class
未打开 Java 模块模式,即所有 [=17=可以使用 class 路径上的 ]es。
<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>