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
...
@TestI haven't used Java in a while so this confuses me a little. I downloaded eclipse and when I try to compile this test file there's 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 because it tries to resolve it to a type.
715 Answers
You need to add junit library to the classpath of your project. There are several choices to achieve it depending on your development setup.
Command line: In the case of command line invocations, you will have to add
junit.jarto the classpath of your application withjava -cp /path/to/junit.jar. Take a look at the answers here.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.
3Right-click on the eclipse project and navigate:
Properties -> Java Build Path -> Libraries -> Add Library -> JUnit -> Junit 3/4
It works on mine.
If you use maven and this piece of code is located in the main folder, try relocating it to the test folder.
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/4Step By Step Instructions here
1you can easily search for a line like "@Test" and then use the quickfix "add junit 4 library to the build path" at this line. i think this is faster than adding junit manually to the project.
2you 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> 1 Seem to Junit jar file is not in path also make sure you are using jdk1.5 or above.
2If you are using Java 9 or above you may need to require the junit dependency in your module-info.java
module myModule { requires junit;
} 1 If using Maven you can context click and run 'Maven/Update Project...'
1In 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'
When you add TestNG to your Maven dependencies , Change scope from test to compile.Hope this would solve your issue..
1I 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
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 importsimport org.junit.Test; import static org.junit.Assert.*;Regards
Update to latest JUnit version in pom.xml. It works for me.
1