1

I am trying to create a modular Spring Boot sample using JMPS which introduced in Java 9.

Currently, I created a standalone Maven module for the testing work.

module greeting.webapp.test {
    requires greeting.webapp;
    requires spring.test;
    requires spring.boot;
    requires spring.web;
    requires spring.boot.starter.webflux;
    requires spring.boot.starter.test;
    requires spring.boot.test;
    requires spring.boot.test.autoconfigure;
    requires org.junit.jupiter;
    requires org.junit.jupiter.api;
    requires org.junit.jupiter.params;
    requires org.junit.jupiter.engine;
    requires org.junit.platform.commons;
    requires org.assertj.core;
    requires mockito.junit.jupiter;
}

And when run the sample test, I got the following errors.

Exception in thread "main" java.lang.IllegalAccessError: class org.junit.platform.launcher.TestIdentifier (in unnamed module @0x6325a3ee) cannot access class org.junit.platform.commons.util.Preconditions (in module org.junit.platform.commons) because module org.junit.platform.commons does not export org.junit.platform.commons.util to unnamed module @0x6325a3ee
    at org.junit.platform.launcher.TestIdentifier.from(TestIdentifier.java:56)
    at com.intellij.junit5.JUnit5IdeaTestRunner.<clinit>(JUnit5IdeaTestRunner.java:86)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:377)
    at com.intellij.rt.junit.JUnitStarter.getAgentClass(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:210)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

And in the test module, I have to move the test scoped deps to compile time to make it work in jmps, how to resolve this issue?

Naman
  • 27,789
  • 26
  • 218
  • 353
Hantsy
  • 8,006
  • 7
  • 64
  • 109

1 Answers1

0

There is no easy way, or no good way at all, imho.

The problem you are having is that you have not configured maven surefire correctly. You can try that - I did and somehow was unlucky, but I did not invest too much time in making it work (neither do I think that will work, but that is a different problem). Instead I configured surefire plugin manually, via:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <argLine>
                    --add-exports org.junit.platform.commons/org.junit.platform.commons.util=ALL-UNNAMED
                    --add-exports org.junit.platform.commons/org.junit.platform.commons.logging=ALL-UNNAMED
                </argLine>
            </configuration>
        </plugin>

Overall, I like gradle approach more. You can read on my experience with it here. I also don't think that IDEs (Intellij in my case) have yet proper support to run a single (maven based project) test, unlike gradle. But, to be fair, I only tried that against your repo, so far...

You can also read about a rather neat approach that gradle has taken when you need to declare modules here, with their dedicated plugin.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • This could be a fallback way for those jars not ready for java modules, JUnit itself is jmps modularized, I still encoutered other issues there, I will update you when I focus on it later. – Hantsy Nov 14 '20 at 01:16