Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit tests in Eclipse on Java 9 modular project

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)?

like image 510
Cromax Avatar asked May 19 '18 15:05

Cromax


People also ask

How do you write JUnit test cases in Eclipse?

Creating a JUnit Test Case in Eclipse java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.

How do I add a JUnit test to a Java project?

Creating a Java ProjectClick File --> New --> JavaProject. Give the project a name and click "Next" not "Finished". Under the "Libraries" tab, click "Add Library", "JUnit", "Next", select "JUnit 4", and click "Finish". Import the homework files into the src folder.

Why my JUnit is not showing in Eclipse?

That kind of J icon filled to a "bubble" means that Eclipse doesn't recognize your project as a Java project, and therefore doesn't provide Java options such as Run as JUnit. Try reimporting the project as a Java Project. Show activity on this post.


1 Answers

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

like image 116
andy Avatar answered Oct 02 '22 13:10

andy