How to compile test code in a modular project?

Alan Bateman Alan.Bateman at oracle.com
Thu Nov 9 08:29:41 UTC 2017


On 08/11/2017 21:03, Till Brychcy wrote:
> A common source structure (e.g. used by maven and gradle) is that tests are in separate source directory with separate output directory
> and are in the same packages as the code that is being tested, so the tests can access code elements that have package visibility.
>
> Also, test dependencies like junit should not be referenced in the module-info.java, so „main" code cannot accidentally access them.
>
> The question is, how to compile the tests with javac (version 9.0.1)?
> :
>
> Is compiling code test code (or any code to be used with --patch-module) as part of the module actually possible with javac?
>
Yes, --patch-module is needed to compile the tests "as if" they are part 
of the module, e.g.

    javac --module-path classes:junit.jar --patch-module m=src/test/java 
--add-reads m=junit -d test-classes \
        src/test/java/p1/P1Test.java

Are you using Maven? If so then the maven-compiler-plugin already does 
this for you when the project is a module.

-Alan



More information about the jigsaw-dev mailing list