8

I'd like to run tests with JUnit 5 on Java 9 modular project in Eclipse, with no Maven, Gradle or all that fancy stuff. So I have src/main/java path where module-info.java and module's packages live and also src/test/java where all the test classes are. Id est business as usual, prior to the Jigsaw module system. I have Eclipse Oxygen.3a (4.7.3a) an Java 10.0.1.

I've seen some video from Eclipse showing, how to add JUnit test to modular project, but this flabbergasted me deeply: they put required keyword into module-info.java of a module, binding it to JUnit module. Is that actually even correct?

I've seen also all these --patch-module/--add-reads solutions (when we're talking about working in a console) and it seems like it's the proper way to do it, but I have no idea, how to achieve that in Eclipse without binding module under test to JUnit module. Is that even possible in Eclipse (without Maven and s.o)?

Cromax
  • 1,822
  • 1
  • 23
  • 35
  • Till Brychcy explained this quite well in the following answers: https://stackoverflow.com/a/50324391/6505250 and https://stackoverflow.com/a/50408329/6505250 (the problem is JUnit < 5.0.2 which is contained in Oxygen.3a; use a preview build of Photon which will be released in June 27) – howlger May 19 '18 at 16:00
  • So it looks, like it's not possible in Oxygen, but Photon may actually handle it. I'll give it a try then, thanks for the tip! – Cromax May 19 '18 at 16:05
  • 1
    It also works in Oxygen if you add the JUnit >= 5.0.2 JARs to the Java Build Path. But better use Photon which also has classpath separation main vs. test. – howlger May 19 '18 at 16:07
  • JUnit in Oxygen has 5.0.0 version tag and I didn't want to mess with it. I went with Photon. Indeed, in build path settings one can mark if given source folder is a test folder and it is necessary to set separate folders for test and non-test classes (one need to check Allow output folders for source folder in Source tab of Java Build Path) and I can now run JUnit tests (and as it seems to be obvious, JUnit library must be placed in classpath, not modulepath). So it works in Photon. I probably won't tweak JUnit version in Oxygen, but maybe someone will try to. Thanks again! – Cromax May 19 '18 at 17:00

1 Answers1

2

I tried to solve this problem for quite a while, too. My approach is to add a filter to the source code directory for src/main/java that filters out the module-info.java. This allows to have a different module-info.java in src/test/java. It will be this one that gets copied to the output folder. This way you can run your unit tests from within the IDE and use the other one for the final build. However, you need to keep the content of the one in src/main/java updated yourself.

Right click on the project > Properties > Java Build Path > Source

Select the src/main/java entry, click Edit > Next > Exclusion Patterns > Add

andy
  • 99
  • 7
  • Does it mean that `src/test/java` classes are modular (because of additional `module-info.java` in it)? So any change in `src/main/java/module-info.java` requires possibly changes to `src/test/java/module-info.java`? Seems a bit off to me, but it sounds like some solution, thou. Yet I'd rather advice switching (if possible) to Photon, the test vs main separation works there great. – Cromax Jul 13 '18 at 18:50
  • There will be only one module in your Eclipse project, because the module-info in main got filtered out. I admit my solution isn't great, it's more a "at least it works"-solution. The thing is that I don't want to add test related entries to the module-info in main. It would be great if Eclipse actually supported to have two module-info.java files, but that required possibly that it also manages two bin-directories. I'm using Photon and I couldn't find such a feature. How did you handle it? – andy Jul 17 '18 at 05:30
  • Indeed (up to now at least) one can have only one module file per project. Yet it is possible to totally get rid of test stuff from module file. In Photon, you use, you need to play with build path settings. First, you need to allow separate output folders per source folder. Then you need to set different output folders for main and test src folders. And finally you need to mark test src folder as TEST folder (overlay icon becomes green). Since then test src folder works as pre-module version (JUnit runs perfectly in there) and main src can be modular. Hope it helped. – Cromax Jul 26 '18 at 09:43