Accepted answer

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.


update to latest junit version in pom.xml. it works for me.


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


note : please get the latest from the following link

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 {

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


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'

add junit libraries


when you add testng to your maven dependencies , change scope from test to compile.hope this would solve your issue..


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 mymodule {
    requires junit;


if using maven you can context click and run 'maven/update project...'

enter image description 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.



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


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


right-click on the eclipse project and navigate to

properties -> java build path -> libraries -> add library -> junit -> junit 3/4

it works for me.

Related Query

More Query from same tag