I have a project where I use reflective access to my classes (e.g. by Spring, but also others). And everything is working fine. Now i tried to add a module-info.java
and suddenly my tests can not find my production code using reflections in IntelliJ IDEA Ultimate 2019.3 with OpenJDK 11.0.2. I've declared my module as an "open module".
- it works fine when I delete the
module-info.java
again - it works fine when I run tests that do not require reflective access
- it works fine when I run the tests using Maven (as far as I can tell)
To reproduce I've set up a new project as follow and it still does not work:
A simple project with basically two files:
- src/com/stackoverflow/test/Main.java (basically just any class)
- test/com/stackoverflow/test/MainTest.java
The MainTest.java
contains the following two simple tests which both pass:
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());
}
}
When I now add a src/module-info.java
with the following content, the second test testingReflections()
fails in IntelliJ IDEA because it only finds the "MainTest".
open module com.stackoverflow.test {
exports com.stackoverflow.test;
}
How can I enable reflective access in my tests to my production code in IDEA?
The command which gets executed is something like that:
/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
Ant the error is about this:
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