0

I have Eclipse project in which i use Maven. I also use surefire plugin for tests. I wrote some test classes and tried to run mvn test, however i get this exception:

java.lang.reflect.InaccessibleObjectException: Unable to make static void 
dataaccesslayer.desktopapp.DesktopAppCRUDTest.setupTestingDatabase() accessible: 
module registry.dataaccesslayer does not "opens dataaccesslayer.desktopapp" to unname
d module

I see that i am calling some static method (of class in src/main/java) in test class, which is causing this problem.

So i tought i could just write: opens dataaccesslayer.desktopapp in module-info.java, however i can not write that, because module-info is in src/main/java and not src/test/java.

So is there a way to open test package in module-info.java? If not, what could i do to make test work when i am calling static methods of non-test classes? Is that a bad approach to call static methods of non-test classes in test classes?

Wortig
  • 963
  • 2
  • 11
  • 37
  • 1
    It does not really matter if a test source is in a different directory. Open the package in `module-info.java` and copy the `module-info.class` after `test-compile` to the root directory of generated test classes. – Aniket Sahrawat Apr 26 '21 at 20:04
  • 2
    You can add the option to open modules while testing like [this](https://github.com/RovoMe/JDrum/blob/master/jdrum-datastore-simple/pom.xml#L47) – Roman Vottner Apr 26 '21 at 23:17
  • Here you can get more information: https://sormuras.github.io/blog/2018-09-11-testing-in-the-modular-world.html – Thiago Henrique Hupner Apr 30 '21 at 12:11

1 Answers1

1

You can add reflective access for testing, without altering the production version of module-info.java, by using the Runtime Option --add-opens in the build command line for compiling and building unit tests.

Basically this has the same effect as patching the module-info.java file from the command line just for the purposes of unit test build. Build tools such as Gradle etc have packaged this up to lessen the ugly PITA it otherwise is.