反射访问在IntelliJ IDEA中的模块化(Java 9+)应用程序中的测试中不起作用



我有一个项目,我使用反射访问我的类(例如,通过Spring,但也通过其他(。一切正常。现在我尝试添加一个module-info.java突然我的测试无法使用 IntelliJ IDEA Ultimate 2019.3 中的反射和 OpenJDK 11.0.2找到我的生产代码。我已将我的模块声明为"开放模块"。

  • 当我再次删除module-info.java时它工作正常
  • 当我运行不需要反射访问的测试时,它工作正常
  • 当我使用 Maven 运行测试时,它工作正常(据我所知(

为了重现,我已经设置了一个新项目,如下所示,但它仍然不起作用:


一个基本上有两个文件的简单项目:

  • src/com/stackoverflow/test/Main.java(基本上是任何类(
  • test/
  • com/stackoverflow/test/MainTest.java

MainTest.java包含以下两个简单的测试,它们都通过:

package com.stackoverflow.test;
imports...
public class MainTest {
@Test
public void testingAccess() {
var actual = new Main();
assertNotNull(actual);
}
@Test
public void testingReflections() {
Set<String> allClasses = new Reflections("com.stackoverflow.test", new SubTypesScanner(false)).getAllTypes();
for (String oneClass : allClasses) {
System.out.println(oneClass);
}
assertEquals(2, allClasses.size());
}
}

当我现在添加包含以下内容的src/module-info.java时,第二个测试testingReflections()在 IntelliJ IDEA 中失败,因为它只找到"主测试"。

open module com.stackoverflow.test {
exports com.stackoverflow.test;
}

如何在测试中启用对 IDEA 中生产代码的反射访问?


执行的命令是这样的:

/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=56350:/Applications/IntelliJ IDEA.app/Contents/bin" --patch-module com.stackoverflow.module=out/test/ModuleInfoTest --add-reads com.stackoverflow.module=ALL-UNNAMED --add-opens com.stackoverflow.module/com.stackoverflow.example=ALL-UNNAMED --add-modules com.stackoverflow.module -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit5-rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar:out/test/ModuleInfoTest:lib/junit-4.12.jar:lib/hamcrest-core-1.3.jar:lib/reflections-0.9.12.jar:lib/javassist-3.26.0-GA.jar" -p out/production/ModuleInfoTest com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.stackoverflow.example.MainTest

蚂蚁的错误是关于这个的:

Found class: com.stackoverflow.example.MainTest
java.lang.AssertionError: 
Expected :2
Actual   :1
<Click to see difference>

at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:631)
at com.stackoverflow.module/com.stackoverflow.example.MainTest.testingReflections(MainTest.java:33)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Process finished with exit code 255

您的情况似乎与 2019.3 (https://youtrack.jetbrains.com/issue/IDEA-171419( 中的模块化系统修复有关,它允许运行模块感知测试。 请考虑以下不同的解决方法:

  • 在模块信息中导出所需的模块;
  • 将导出添加到运行配置;
  • 使用自己的模块信息创建专用测试模块。

还请考虑以下问题: https://youtrack.jetbrains.com/issue/IDEA-222831(支持模块信息测试配置文件(。

以下是关于类似问题的讨论: https://youtrack.jetbrains.com/issue/IDEA-228999

相关内容

最新更新