Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 9 Modules and JUnit 4

Eclipse oxygen; windows 7; JDK 9 final from 9, 21; JUnit 4.12 and an existing application. As starting point the application can be compiled, executed and all JUnit Tests shows green. Now we use eclipse to generate the file module-info.java. The outcome looks like:

module ch.commcity.topsort {
   exports ch.commcity.topsort;

   requires junit;
}

But with the error: junit cannot be resolved to module. The question is: How to tell the file that junit does not define any module and it should be run in compatibility mode?

like image 560
juerg Avatar asked Sep 27 '17 20:09

juerg


2 Answers

How to tell the file that junit does not define any module and it should be run in compatibility mode?

Your question seems to be based on several misconceptions:

  • You can not tell module-info.java whether JUnit defines a module or not. If a module says it requires another module then the compiler expects that module to be present - no way around that.
  • Whether JUnit 4 comes as a module or not is not overly important - as long as you place it on the module path, it will end up being treated as a module (possibly as an automatic one).
  • There is no "compatibility mode". You can continue to write code as you did before the module system (almost), but once you're creating module declarations you need to play by its rules.

I recommend to give the outstanding State of the Module System a thorough read and then ask yourself what exactly you are trying to accomplish. Are you really trying to create a module that depends on JUnit? Or was that just accidental because you use its API for testing. If the latter, you should not depend on it - instead your IDE / build tool needs to figure out how to compile and run your tests.

Expansion on "a module that depends on JUnit"

The module system does not classify dependencies as "compile" or "test" - if a module requires another module, it has to be present, always. That means a module that requires junit would force the presence of JUnit. Unless the module provides testing-related features, that is most certainly wrong.

In other words, requires junit is akin to adding JUnit to a project's POM, using the compile scope.

like image 115
Nicolai Parlog Avatar answered Oct 12 '22 23:10

Nicolai Parlog


First, please update your Java 9 support for Eclipse Oxygen or use the latest available release candidate build for Eclipse 4.7.1a (to be released on October 11, 2017).

To add a library or a container to the modulepath of a Java 9 project in Eclipse, open the project's Java Build Path dialog. On the Libraries tab, select the node Modulepath and add your library to it. Make sure to first remove that library from the Classpath node if it is already present there.

As mentioned by others, in your case, the JUnit libraries will be considered as automatic modules.

Modulepath node

like image 29
Noopur Gupta Avatar answered Oct 13 '22 01:10

Noopur Gupta