Eclipse + m2e + junit5 - already possible?

143 Views Asked by At

Tried to get Eclipse 2018-09 + Patch with Java 11 support, m2e, and junit5 working together.

As recommended in junit5-modular-world example I introduced a second module-info.java under test/java.

The reaction of Eclipse was astonishing to me:

I could not save that file after changing it. It was saved only by closing Eclipse at all. However, re-opening, bewildered Eclipse. It cannot show any details of the project hosting multiple module-info.java, just the project name.

Probably Eclipse identifies one project with one Java module, while mvn test compiles and executes obviously a different module than the one created by mvn install.

Experienced a lot of options I can think of. Currently I had to give up and fall back to junit 4.12.

Do you know of a better solution?

1

There are 1 best solutions below

12
Till Brychcy On

A secondary module-info.java in the test source folder is not supported by Eclipse at this time (but its behaviour if you try to do that should probably be improved).

For now, you probably won't need it at all:

Maven puts dependencies that are mentioned in the module-info.java on the module path, all others (e.g. test-only dependencies like junit) on the class path, so they become part of the unnamed module. When tests are compiled, command line options are added, so the test code that is treated as part of the module in the main source folder still can read the unnamed module (by adding --add-reads modulename=ALL-UNNAMED), so junit is visible to the test code.

Eclipse Photon and later also supports this behaviour.

Some background regarding the secondary test module-info.java: maven-compiler-plugin supports this since version 3.8 (see https://www.mail-archive.com/[email protected]/msg00866.html, implemented in issue https://issues.apache.org/jira/browse/MCOMPILER-341), but I'm not aware that a matching maven-surefire-plugin has been released, so I think you currently wouldn't be able to run these kinds of tests with maven.

Implementing support for a secondary test module-info.java in Eclipse may be possible, as long as it is a strict superset of of the primary module-info.java in the main source folder, or maybe as long as they specify the same module and their contents would get merged as in the "pro" build tool https://github.com/forax/pro. But nobody has worked on that yet.

What will probably be never supported in Eclipse, is to have a secondary test module-info.java that specifies a different module as Eclipse has the assumption that one java project belongs to only one module. But that shouldn't matter, as these tests can only use public and exported code of the main sources, so they can simply be put into their own maven module.