How do I add my tests to my production code at test-runtime so that both are in the same Java 9 module and can access each other using reflections?
I have tried so far:
- Remove the Java 9 modularity (actually the
module-info.java
) → it worked perfectly, but is not what I'm looking for. - Move my tests to a dedicated module (and therefore also package) → it worked perfectly, but is not what I'm looking for. (I want my Unit-Tests near my code.)
- Use
--patch-module
to virtually add another folder (to the one specified using--module-path
) → it worked with "normal" code but not with reflections, it doesn't find the classes specified by--module-path
.- Specify both my test and production code using
--patch-module
→ it only finds classes in the folder I specify first. - Explicitly add
--add-opens mymodule/mypackge=mymodule
or...=ALL-UNNAMED
to open it for reflection → doesn't look to have any effect.
- Specify both my test and production code using
So my full test line is:
java \
--patch-module com.stackoverflow.examplemodule=ModuleInfoTest:ModuleInfoExample \
--module-path ModuleInfoExample \
--add-opens com.stackoverflow.examplemodule/com.stackoverflow.examplepackage=com.stackoverflow.examplemodule \
--add-opens com.stackoverflow.examplemodule/com.stackoverflow.examplepackage=ALL-UNNAMED \
--module com.stackoverflow.examplemodule/com.stackoverflow.examplepackage.Main
I'm in a directory containing the following subdirectories and files:
- ModuleInfoExample/module-info.class
- ModuleInfoExample/com/stackoverflow/examplepackage/Main.class
- ModuleInfoTest/com/stackoverflow/examplepackage/AnyClass.class
I'm using:
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
As I learned from the approved answer, my problem was not really "access other classes", as I phrased it. But more like find them (by scanning classpath/modulepath). However this part is already answered in this other StackOverflow question.