1

Evnironments

  • Mac OS High Serial 10.13.6
  • Version: 2018-09 (4.9.0)

Hi, I am having trouble create Junit Test Case with Eclipse. I've google this error, but all the posts tell me that you have to add. As shown below I had.

The error is The import org cannnot be resloved

Somebody know what are the possibilities of my problems ?

https://www.dropbox.com/s/1605sy6t612mp2d/Screenshot%202018-10-04%2019.32.05.png?dl=0

enter image description here

enter image description here

Community
  • 1
  • 1
Naggi Goishi
  • 73
  • 2
  • 8
  • When you expand that JUnit 5 node in the Package Explorer, do you see jars there? Can you find the Test class you're trying to import in one of them (and show it to us)? – nitind Oct 05 '18 at 03:42
  • Thank you just edited the post – Naggi Goishi Oct 05 '18 at 23:13
  • Are you able to add JUnit5 to the module path? You have a file declaring your package as a module, and I’m guessing it’s not mentioning that it requires JUnit5 *there*. – nitind Oct 06 '18 at 04:30
  • @nitind Thank you. I was not sure what is module path and I've looked up. And ended up solving this problem by making new package for my test. I would post answer for myself later ! – Naggi Goishi Oct 10 '18 at 00:56
  • I am facing the same problem. I don't understand much why. My guess is it goes along with that new module-info thingy. Now I don't know what would be the proper way of using that. I did what you propose bellow. Does not change a thing. It seems like JUnit is not recognized though it is part of the class path (not module path). Can you give the steps of what you did to solve this? – 猫IT Oct 11 '18 at 15:50
  • @猫IT Are you sure I have followed the setup ? https://www.eclipse.org/community/eclipse_newsletter/2017/october/article5.php – Naggi Goishi Oct 12 '18 at 00:51
  • Yes I have everything setup correctly as per the good old proven way and also as per what is written in the article but it does not work in my case. I followed the various ideas mentioned here and none fixed the issue. One evolution I had is by adding requires junit in the module-info.java file. Error symbols got away in package explorer yet the source still behave the same. I'm pretty certain it has to do with this module-info not properly setup. I remade my project using Java 8 so no module-info and it works like a charm. Now I need to make it work on Java 9+ with module-info. Regards. – 猫IT Oct 12 '18 at 02:45
  • @猫IT Hi, sorry for not able to help you but I have no idea what is the problem you have. I just get started to Java a month ago. Well, what I can say for that is Java programmer don't link to require junit in module-info.java as the the post said below https://stackoverflow.com/questions/50321602/in-eclipse-what-is-the-difference-between-modulepath-and-classpath – Naggi Goishi Oct 30 '18 at 20:18
  • Thank you for your answer anyway. I'm just trying to learn about this modularity thing because it is the future. From now on Java will support/maintain only the 2/3 last versions of Java which means sooner or later we will be forced to deal with it. while I understand the concepts of Jigsaw i don't understand how to use it and no tutorial or ressource I found could help me tough I saw a video about Eclipse that seems to show JUnit used with module-path. I need to find out. Again thanks for the answer. – 猫IT Oct 31 '18 at 21:30

1 Answers1

2

With @nitind help, I could solve the problem.

Because I did not know what is module path and google it. I ended up reading this post and in that post it says

There is one special case: If you have a module-info.java in your project and have test code in your project, you usually don't want to mention test dependencies like junit in the module-info.java. There are two solutions for this:

Create a dedicated test module. This has always been the convention for osgi-based projects. Disadvantage is that you can only use public api in your tests

The solution used by maven: Put your test dependencies on the classpath. When compiling test code, maven adds command line options that allow the code in the named module to read the unnamed module (which is not possible via the module-info.java).

So, as it says I made dedicated test module like the picture below.

enter image description here

  1. Right click the test module
  2. Go Build Path > Configure Build Path
  3. Then toggle Contains test sources: No to Yes
  4. Check Allow output folders for source folders
  5. Change the Output Folder to test (or anywhere you want other than you default (bin))

enter image description here

Run the test and the test should work from now.

Naggi Goishi
  • 73
  • 2
  • 8