简体   繁体   中英

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.

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.

Right-click on the eclipse project and navigate to

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

It works for me.

如果您使用 maven 并且这段代码位于main文件夹中,请尝试将其重新定位到test文件夹。

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

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.

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> 

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

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

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

在此处输入图像描述

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.

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'

添加 Junit 库

当您将 TestNG 添加到 Maven 依赖项时,将范围从测试更改为编译。希望这会解决您的问题..

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM