elkshadow5
elkshadow5

Reputation: 459

Intellij “java: package org.junit does not exist”

FULL DISCLOSURE: I know this question has been asked before, but no previous answers actually solved my problem. Do not mark as duplicate please the answer in this question does not actually solve my problem.

I currently have 3 separate projects that use IntelliJ, one each for 3 different university courses. On one of them, JUnit testing works just fine without any issues. On the second project I haven't had to do any JUnit testing so I don't know whether or not it works on there. On a new project I created, it doesn't work at all and gives me about 50 errors all along the lines of java: package org.junit does not exist.

I saw this question and followed the steps in the accepted answer to try and fix the problem. My project dependencies now looks like this:

enter image description here

I still get that error. My project directory looks like this:

enter image description here

Do I have to change the scope of the .jar files or the JUnit5.2 modules? My other project that JUnit is "accepted" on currently has JUnit5.0 listed in the modules page, and it shows up twice- once with the scope set to Compile and once with the scope set to Test. Is there a huge difference between JUnit5.0 and JUnit5.2 and how they work? Do I have to add JUnit5.2 again but with the Compile scope? If so, how?

This question doesn't work either since it has to do with Android development and from what I can tell they have their project(s) set up completely different from me.

Upvotes: 5

Views: 20927

Answers (4)

A.Mushate
A.Mushate

Reputation: 455

This is how I resolved it (Intellij) Right click the test directory -> Mark Directory as -> Test Sources Root

Upvotes: 2

vuzz
vuzz

Reputation: 61

If your project is a maven project, probably you marked the src Folder as a Source Folder, and IntelliJ is trying to compile the Test Units too.

Open your module settings and unmark src Folder as Source Code Folder

Upvotes: 6

Zia
Zia

Reputation: 755

Use this code in your dependencies segment of the build.Gradle file.

testImplementation 'junit:junit:4.12'
implementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

Upvotes: -1

Dilip Agheda
Dilip Agheda

Reputation: 2547

Thanks for uploading to github.

I imported your project to IntelliJ and hit build. I also got around 10 errors complaining about jUnit.

Solution: - Go to the import statements for which you get error message that no package jar file is available. put cursor there and press ALT+ENTER. You will get options like this: enter image description here

  • Choose the first option and let the intelliJ studio add to classpath. you will get like this:

enter image description here

enter image description here

After that build was successful and all the errors were gone. Also, I learnt that if you add each package manually then chances of errors will be high. best thing is to use maven package management, give it the package you want and then it will figure out all the dependancies that comes with it and download to your project. Also, this has nothing to do with jUnit version. Any decent recent version will do. Hope this helps.

Upvotes: 8

Related Questions