Junit with jigsaw project
Answered
What is the proper project layout when using module-info.java and JUnit? I still can create test directory in my module, mark it as "Test sources", and run tests from there. But the code is all highlighted with errors like "Symbol is declared in module 'org.junit.jupiter.api' which current module does not depend on" which is very annoying. Obviously I do not want to add junit dependency in the production module-info.java (even when trying to do so, you also need to specify dependency scope "Compile" instead of "Test" for JUnit library in the module dependencies, which is also not good). Is there any way to make it working nice like with pre-Java9 non-modular projects?
Please sign in to leave a comment.
At the moment, IDEA does not support running tests from a module path (watch IDEA-171419 for progress).
The answer depends on a kind of tests. For unit tests, the most practical place is a test root of a module where production classes live. For integration tests, it would be a test root of a separate module. Something like this:
project/
a_module/
a_module.iml
src/
module-info.java
pkg/svc/AService.java
test/
pkg/svc/AServiceUnitTest.java
integration_tests/
integration_tests.iml
test/
pkg/integration_tests/IntegrationTest.java
Once IDEA-171419 is resolved, you'd be able to add a module-info file to integration_tests module.
Thanks for your answer.
Actually, I am about unit tests and I used the layout as you described, and I have a problem with symbols resolving, described in the first message.
So it was
project/
a_module/
a_module.iml
src/
module-info.java
pkg/svc/AService.java
test/
pkg/svc/AServiceUnitTest.java
and AServiceUnitTest.java is all highlighted unless you include JUnit in module-info.java as well as specify it "Compile" scope in a_module.iml.
So far I have found an acceptable workaround:
project/
a_module/
a_module.iml
src/
module-info.java
pkg/svc/AService.java
test/
src/
pkg/svc/AServiceUnitTest.java
a_module.test.iml
So I moved unit tests in a separate module (not jigsaw) which referers to the application module. It still can be a subdirectory of a_module and if named "a_module.test" is properly displayed as its submodule in the project modules. So I can live now with that solution.
This could happen if test/ is a source root instead of being a test one.
Nope, the test/ directory is marked as tests root. Otherwise it would not work because JUnit dependency scope is "Test" in the module dependencies. Just IDEA unit tests integrated runner and editor have different opinions on test code environment (jigsaw/not-jigsaw) which is IDEA problem indeed.
Then it should work.
Could you please attach a sample project?
https://drive.google.com/open?id=1ljEW5TnikBoIVprbZ2xJ9DeKKq7wQdke
The dependency org.junit.jupiter:junit-jupiter-api:5.2.0 is in my global libraries. By the way I also have another problem with this project (and in all my projects in IDEA) - for some reason the tests are not compiled until I copy junit library to the project libraries (by "Copy to project libraries..." action in global libraries screen). At the same time editor properly sees it without this step - highlighting is removed, autocompletions and suggestions work. I noticed such strange behaviour on some libraries, not only JUnit, but not for all, most are working properly just from global libraries. I have this problem on both Mac and Linux (did not try on Windows).
Here I removed the module-info.java and junit from project libraries, editor is happy but the project cannot be built:
Plain Java projects suffer from this as well for me.
Turned out to be a problem in Kotlin. I'll let them know.
Related issue to track: https://youtrack.jetbrains.com/issue/KT-26037.
Nice, thanks! Does anyone also see the problem with some global libraries being ignored by compiler, described above and visible in the project attached?
> for some reason the tests are not compiled until I copy junit library to the project libraries (by "Copy to project libraries..." action in global libraries screen).