Naggi Goishi
Naggi Goishi

Reputation: 73

Can not import JUnit with Eclipse

Evnironments

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

Upvotes: 1

Views: 934

Answers (1)

Naggi Goishi
Naggi Goishi

Reputation: 73

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.

Upvotes: 2

Related Questions