score:208

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.

score:-3

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

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

score: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

score: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.

score:1

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

score:1

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

score:2

seems that the junit .jar file is not in the path. also, make sure you are using jdk1.5 or above.

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

score:2

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

enter image description here

score: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.

score:5

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> 

score: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

score:7

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

score: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.


Related Query

More Query from same tag