The import org.junit cannot be resolved Ask Question

The import org.junit cannot be resolved Ask Question

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.

ベストアンサー1

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.

おすすめ記事