140

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.

Sae1962
  • 1,122
  • 15
  • 31
SKLAK
  • 3,825
  • 9
  • 33
  • 57

16 Answers16

214

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.

Sae1962
  • 1,122
  • 15
  • 31
  • 2
    @NishantShreshth how to add this in a `maven` project? – Kasun Siyambalapitiya Sep 25 '17 at 10:30
  • I added JUnit 4 via Properties -> Java Build Path, but every now and then Eclipse still shows me this error, though running the tests is possible without problems. Restarting Eclipse resolves the problem - for a while. Any explanation for this? – Thomas W Jan 15 '18 at 13:27
  • Does not work for me. As soon as the project is modular, all the buttons in the Libraries tab are grey. (With eclipse 2019-09 and java 11.0.4). – Gyro Gearloose Dec 09 '19 at 12:42
15

Right-click on the eclipse project and navigate to

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

It works for me.

Sae1962
  • 1,122
  • 15
  • 31
fastforward
  • 169
  • 1
  • 2
7

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

Searene
  • 25,920
  • 39
  • 129
  • 186
  • 3
    @VigneshrajSekarbabu Because people usually set `junit`'s scope as `test`, which means they will only work in the test folder. – Searene Dec 16 '18 at 06:47
  • Thank you @searene.. I tried to have my TestBase in the src/main/java but then was not able to get the testNG annotations. so moved to src/test/java. But now, i am trying to create a RetryAnalyzer in src/main/java and IRetrAnalyzer is in org.testng which i am not able to get in src/main/java classes. any idea what could solve this ? – Vigneshraj Sekarbabu Dec 16 '18 at 08:53
  • @VigneshrajSekarbabu One way is to check your test-related dependencies' scope, make sure they are not `test`, so that you could use everything inside `/src/main/java`. – Searene Dec 16 '18 at 10:54
6

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

Mehdi LAMRANI
  • 11,289
  • 14
  • 88
  • 130
sarthakgupta072
  • 451
  • 6
  • 13
  • For me this only worked. I was performing remote debugging of my web application, so eclipse had some issues with integrating junit. With regard to maven, yes maven imports junit and will run the tests when we'll run mvn clean install. Not an expert, but this is what I think that happens or atleast happened with me. – sarthakgupta072 Dec 12 '18 at 12:03
6

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> 
Veera Reddy
  • 61
  • 1
  • 1
5

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.

Sae1962
  • 1,122
  • 15
  • 31
anion
  • 1,516
  • 1
  • 21
  • 35
2

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

Sae1962
  • 1,122
  • 15
  • 31
zaffargachal
  • 802
  • 7
  • 21
  • Not just the jar, OP should run it as a JUnit test (maybe OP will ask how to run the Java class if it doesn't have a `public static void main(String[] args)` – Luiggi Mendoza Feb 27 '13 at 06:30
  • Didn't know how to link the JUnit. I figured it out. Right click the project name, and link JUnit. Thanks. – SKLAK Feb 27 '13 at 06:31
2

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

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
2

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;
}
janDro
  • 1,426
  • 2
  • 11
  • 24
  • i'm pretty sure the issue I face which is the same as the original question is linked to this new module mechanism. Now i did add this line to my existing module-info.java and there is no more red cross in the package explorer but in the source everything remains the same. – 猫IT Oct 12 '18 at 00:14
2

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

enter image description here

ABC123
  • 1,037
  • 2
  • 20
  • 44
  • When I do this, module-info.java is filled with errors and lambdas are not recognized. Eclipse/maven have reverted the library and java version to 1.5, and I do have org.apache.maven.plugins maven-compiler-plugin 3.8.0 11 in my pom – Gyro Gearloose Dec 09 '19 at 12:37
1

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.

Dharu Ravi
  • 21
  • 3
1

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

Ajay
  • 11
  • 1
0

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

Ralf Renz
  • 1,061
  • 5
  • 7
0

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

0

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

holvold
  • 43
  • 6
-3

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

RKM
  • 27
  • 5
  • this answer would be better with more details on how to do that - i.e. what code to paste, how to figure out the latest version, how to search for the code online to get the right text, etc. – LightCC Dec 21 '18 at 19:15