When i try to run my test i get the following Errors: I looked up on google and found somthing on google but this didn't help.

Error:(3, 24) java: package org.junit does not exist Error:(3, 1) java: static import only from classes and interfaces Error:(5, 17) java: package org.junit does not exist Error:(6, 17) java: package org.junit does not exist Error:(12, 10) java: cannot find symbol symbol: class Before location: class edu.kit.ipd.swt1.SimpleEditMeTest Error:(17, 10) java: cannot find symbol symbol: class Test location: class edu.kit.ipd.swt1.SimpleEditMeTest [...] 

My test code:

package edu.kit.ipd.swt1; import static org.junit.Assert.assertNotNull; import org.junit.Before; import org.junit.Test; public class SimpleEditMeTest { private EditMe editMe; @Before public void setUp() throws Exception { editMe = new EditMe(); } @Test public void test() { assertNotNull(editMe.getFoo()); } } 

Screenshot of the whole project

Run configurations

Dependencies i.stack.imgur. com/OiQWU.png (Can't post more than 2 links)

1

11 Answers

This worked for me:

  1. Right click pom.xml
  2. Select Maven
  3. Reload project
1

For anyone ending up here using maven, a couple of things to check:

  • (Sanity check) Have you added junit as a dependency in your pom.xml?
  • Have Intellij accepted your module as a maven module? Try Right clicking the module/project and select Add as Maven Project or perhaps Maven->Reimport
  • Are you using explicit version in your dependency declaration in your pom file? Like <version>4.12</version>. Try that. (You might not want to this because you want the version to be decided by a parent module. Read on.)
  • Have you remembered to add a dependencyManagement? Maven need it to resolve the correct version number if you don't want to have it explicitly in the current project. For me it refers to the parent project. Like this:
<dependencyManagement> <dependencies> <dependency> <groupId>com.myorg</groupId> <artifactId>myproject-parent</artifactId> <version>${myproject.version}</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> 
2

I had the same problem. In order to fix it I had to Open Module Settings for my project and manually add jar Dependencies junit-4.12.jar and hamcrest-core-1.3.jar which are contained in the IntelliJ installation lib directory.

3

You are trying to run your test so you need to set the scope of JUnit to "compile" same as in the picture:

1

1

The solution to this is to add junit library as dependency to the project. This can be done by adding junit to global library and then hovering over the error(junit word) and right clicking to add junit to class path. alt+shift+ctrl+s to get project settings in the same either add junit to global library or to project section as mentioned by rob in his answer. once you add it here the red bulb icon would give you option to resolve the error by adding it to class path.

1

I had the same problem. Adding JARs made no difference. I solved it by adding library (not JAR) dependency for Junit 5.3

You can go to project structure and edit dependencies by changing the junit dependency to compile from just test

intelij community edition

For my case, I have also added junit library dependency manually to the project.

I'm using IntelliJ 2020.2

And it works !

capture image adding junit library to intellij

I see this as a bug from intellij as they are not able to sometimes load the dependencies with the test scope during the build operations. You can see that this error will only come for dependencies with the test scope.

Adding the JUnit dependency to build.gradle solved the issue

dependencies { testCompile 'junit:junit:4.12' compile 'junit:junit:4.12' } 

If you are using Jupiter with Junit5 testing, Then you need 2 things: -> Jupiter-junit-api -> Jupiter Engine

<!-- --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.1</version> <scope>test</scope> </dependency> <!-- --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.7.1</version> <scope>test</scope> </dependency> 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy