0

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
Itchy
  • 2,263
  • 28
  • 41
  • 1
    You may want to check using `mvn -X` what exact options they pass to the JVM. I suspect that the package split (between main and test code) is handled using some variant of `--patch-module`. Perhaps your IDE uses a different set of JPMS magic. Do they allow you to see the effective command line of test invocation? Comparing both command lines should give some clues. – Stephan Herrmann Jan 26 '20 at 21:28
  • I've tried it with `mvn -X` but I only see it's being launched with/by Surefire... – Itchy Jan 26 '20 at 22:49
  • But in IntelliJ I can see the command and simplified it to: `java --add-opens com.stackoverflow.test/com.stackoverflow.test=ALL-UNNAMED --add-reads com.stackoverflow.test=ALL-UNNAMED --add-modules com.stackoverflow.test -classpath "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 --patch-module com.stackoverflow.test=out/test/ModuleInfoTest org.junit.runner.JUnitCore com.stackoverflow.test.MainTest`. When I run this in the command line it also fails... – Itchy Jan 26 '20 at 22:49

1 Answers1

0

Your case seems to be related to modular system fix in 2019.3 (https://youtrack.jetbrains.com/issue/IDEA-171419), it allows to run module aware tests. Please consider following different workarounds:

  • export the needed module in module-info;
  • add export to Run configuration;
  • create dedicated test module with its own module-info.

Please also consider following the issue: https://youtrack.jetbrains.com/issue/IDEA-222831 (support module-info.test configuration file).

And here is the discussion on similar issue: https://youtrack.jetbrains.com/issue/IDEA-228999

Olga Klisho
  • 1,206
  • 5
  • 7
  • Thanks for your reply, however I don't think the problem is a missing export as it can access the classes, it just doesn't find them using reflections. – Itchy Jan 30 '20 at 19:36
  • Can you please share the output with the error + classpath etc. Thanks – Olga Klisho Feb 03 '20 at 16:04
  • I've updated the questions, see the last part. Thank you! – Itchy Feb 04 '20 at 18:03
  • Please check the v2019.3.3 and later IntelliJ IDEA versions (https://confluence.jetbrains.com/display/IDEADEV/IDEA+2019.3+latest+builds). The option "Use module path" in "Run configuration" is added there as the fix for: https://youtrack.jetbrains.com/issue/IDEA-227983. I've reproduced the issue with the code that you provided, disabling option should help. – Olga Klisho Feb 07 '20 at 12:25