Hello Jochen

Le 09/04/2026 à 23:30, Jochen Wiedmann a écrit :
My impression is, that unit tests in src/test/java, as executed by
surefire, share a module with the code, that's being tested (in
src/main/java). Which is good, in general, because it permits access
to the implementation details, that are being tested. Is that correct,
so far?

Correct, in order to access package-private classes or members, the test code need to be in the same package, which require that the test code is also in the same module. For adding test code to a module, the `--patch-module` option must be used both at compile time and at test runtime.


However, there is another point: It seems important to me, that access
from external modules to my own works as expected. I would like to
ensure that by running some tests as a separate module. Is that a
valid concern? Is that possible, and how?

It is not possible to access package-private classes or members from another module, except by using the `--patch-module` option. But that option should be used only for testing purposes or as temporary patches, not in production.

An alternative is to put the classes and methods that you want to share as public members in another package, then export that package using qualified export. For example, if a module A provides a package `foo` and if you want that only module A and B can access that package `foo`, the `module-info.java` file of module A can contains the following declaration: `export foo to B;`. It is the way to allow access to internal classes to only the modules of your choice.

It is not always easy to do Java modules in Maven 3, because Maven 3 tries to make automatic choices. This is nice when the choices are what we want, but it becomes difficult otherwise. Maven 4 tries to give control back to developers, but the work is not finished. In particular, while the Maven Compiler plugin is ready, we have not yet updated the Maven Surefire plugin.

    Martin



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to