The import org.junit cannot be resolved
JavaEclipseJava Problem Overview
I need to solve a java problem for an interview, and they have sent me the test class. It starts with
import org.junit.Before;
and also has the following syntax at places:
@RunWith(JUnit4.class)
...
@Before
...
@Test
I haven't used Java in a while so this confuses me a little. I downloaded eclipse and when I try to compile this test file there's errors at the import and at the @ signs. The import error throws:
The import org.junit cannot be resolved.
And the @RunWith is not even recognized because it tries to resolve it to a type.
Java Solutions
Solution 1 - Java
You need to add junit
library to the classpath of your project. There are several choices to achieve it depending on your development setup.
-
Command line: In the case of command line invocations, you will have to add
junit.jar
to the classpath of your application withjava -cp /path/to/junit.jar
. Take a look at the answers here. -
Using eclipse: Eclipse distributions are bundled with this library and this is how you can use it for your project. Right click on the eclipse project and navigate:
> Properties -> Java Build Path -> Libraries -> Add Library -> JUnit -> > Junit 3/4
In the scenarios where you want to use a different version of the jar, instead of clicking on Add Library above you should click on Add External Jar and locate the library on the file system.
Solution 2 - Java
Right-click on the eclipse project and navigate:
> Properties -> Java Build Path -> Libraries -> Add Library -> JUnit -> > Junit 3/4
It works on mine.
Solution 3 - Java
If you use maven and this piece of code is located in the main
folder, try relocating it to the test
folder.
Solution 4 - Java
you can easily search for a line like "@Test" and then use the quickfix "add junit 4 library to the build path" at this line. i think this is faster than adding junit manually to the project.
Solution 5 - Java
If you are using eclipse and working on a maven project, then also the above steps work.
Right-click on your root folder.
Properties -> Java Build Path -> Libraries -> Add Library -> JUnit -> Junit 3/4
Solution 6 - Java
you need to add Junit dependency in pom.xml file, it means you need to update with latest version.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Solution 7 - Java
Seem to Junit jar file is not in path also make sure you are using jdk1.5 or above.
Solution 8 - Java
If you are using Java 9 or above you may need to require the junit dependency in your module-info.java
module myModule {
requires junit;
}
Solution 9 - Java
Solution 10 - Java
In case you want to create your own Test Class. In Eclipse go to File -> New -> J Unit Test Case. You can then choose all your paths and testing class setup within the wizard pop-up.
Solution 11 - Java
In starting code line copy past 'Junit' or 'TestNG' elements will show with Error till you import library with the Project File.
To import Libraries in to project:
Right Click on the Project --> Properties --> Java Build Path --> Libraries -> Add Library -> 'Junit' or 'TestNG'
Solution 12 - Java
When you add TestNG to your Maven dependencies , Change scope from test to compile.Hope this would solve your issue..
Solution 13 - Java
I had the same problem right now. My solution: add JUnit to the pom.xml AND remove JUnit from the eclipse project properties (Java Build Path/Libraries).
Solution 14 - Java
If you use Maven with Eclipse then You need to follow the below steps.
1). Add Junit dependency to the pom.xml file
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
Note : Please get the latest from the following link https://mvnrepository.com/artifact/junit/junit
2).On your test class (Ex. AdditionTest.class), you need to annotate with @Test on your test method (Ex. testAdd() )
Note : The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.
public class AdditionTest {
@Test
public void testAdd()
{
// Test code here...
}
The moment/ the first time you annotate as "@Test" the IDE asks whether you need to Add Junit jar files to Class path. Once you accept this it will add the Junit jar file into the class path.
With this you can achieve the following imports import org.junit.Test; import static org.junit.Assert.*; Regards
Solution 15 - Java
Update to latest JUnit version in pom.xml. It works for me.