SKLAK
SKLAK

Reputation: 3965

The import org.junit cannot be resolved

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 for a while, so this confuses me a little. I downloaded eclipse and when I tried to compile this test file, there are 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, as it tries to resolve it to a type.

Upvotes: 142

Views: 438724

Answers (17)

Zhang Buzz
Zhang Buzz

Reputation: 11088

My problem is because somebody put skip true in pom.xml:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <encoding>utf-8</encoding>
                    <skip>true</skip>
                </configuration>
            </plugin>

delete the line of skip true fixed the problem.

Upvotes: 0

holvold
holvold

Reputation: 53

VSCode 2023 (Java v17.0.3 & junit v4.12)

For anyone having this issue in VSCode, I recommend checking this short video on how you perform Testing Java with Visual Studio Code: code.visualstudio.com/docs/java/java-testing/enable-tests

For full documentation check out the official VSCode documentation: code.visualstudio.com/docs/java/java-testing

...and don't forget to add/enable the Java extension in VSCode

Upvotes: 0

user2030471
user2030471

Reputation:

You need to add JUnit library to the classpath of your project. There are several choices to achieve it depending on your development setup.

  1. Command line: In the case of command-line invocations, you will have to add junit.jar to the classpath of your application with java -cp /path/to/junit.jar. Take a look at the answers here.

  2. 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.

Upvotes: 217

zaffargachal
zaffargachal

Reputation: 810

Seems that the JUnit .jar file is not in the path. Also, make sure you are using JDK1.5 or above.

Upvotes: 2

anion
anion

Reputation: 2090

You can easily search for a line like @Test and then use the quick-fix add JUnit 4 library to the build path at this line. I think this is faster than adding JUnit manually to the project.

Upvotes: 5

fastforward
fastforward

Reputation: 169

Right-click on the eclipse project and navigate to

Properties -> Java Build Path -> Libraries -> Add Library -> JUnit -> JUnit 3/4

It works for me.

Upvotes: 15

Hiran Kariyawasam
Hiran Kariyawasam

Reputation: 9

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

Upvotes: 0

Ralf Renz
Ralf Renz

Reputation: 1061

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).

Upvotes: 0

Veera Reddy
Veera Reddy

Reputation: 71

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> 

Upvotes: 7

ABC123
ABC123

Reputation: 1065

If using Maven you can context click and run 'Maven/Update Project...'

enter image description here

Upvotes: 2

Ajay
Ajay

Reputation: 11

When you add TestNG to your Maven dependencies , Change scope from test to compile.Hope this would solve your issue..

Upvotes: 1

sarthakgupta072
sarthakgupta072

Reputation: 636

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

Step By Step Instructions here

Upvotes: 6

RKM
RKM

Reputation: 27

Update to latest JUnit version in pom.xml. It works for me.

Upvotes: -3

janDro
janDro

Reputation: 1466

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;
}

Upvotes: 2

oshan Viduranga
oshan Viduranga

Reputation: 21

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'

Add Junit Libraries

Upvotes: 2

Dharu Ravi
Dharu Ravi

Reputation: 21

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.

Upvotes: 1

Searene
Searene

Reputation: 27654

If you use maven and this piece of code is located in the main folder, try relocating it to the test folder.

Upvotes: 9

Related Questions