导入org.junit无法解析 [英] The import org.junit cannot be resolved

查看:1529
本文介绍了导入org.junit无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解决一个java问题的面试,他们已经寄给我测试类。它以

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

我在一段时间内没有使用Java,所以这让我有点困惑。我下载了eclipse,当我尝试编译这个测试文件时,导入和@符号都有错误。导入错误抛出:

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

@RunWith甚至无法识别,因为它尝试将其解析为类型。

And the @RunWith is not even recognized because it tries to resolve it to a type.

推荐答案

您需要将 junit 库添加到项目的类路径。根据您的开发设置,有几个选择可以实现。

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. 命令行:在命令行调用的情况下,您必须使用 java -cp / path / to / junit将 junit.jar 添加到应用程序的类路径中。罐。请查看此处的答案。

  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.

使用eclipse :Eclipse发行版与该库捆绑在一起,这是您可以将其用于项目的方式。右键单击eclipse项目并导航:

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:




属性 - > Java构建路径 - >库 - >添加库 - > JUnit - >
Junit 3/4

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

在要使用的场景中不同版本的jar,而不是点击添加库,您应该点击添加外部Jar ,并在文件系统上找到库。

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.

这篇关于导入org.junit无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆