🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 如何在 Eclipse 中下载和安装 JUnit > 原文: [https://www.guru99.com/download-installation-junit.html](https://www.guru99.com/download-installation-junit.html) 安装 Junit 是一个 6 部分的过程。 下面详细解释- ## 第 1 部分)安装 Java JUnit 是一个[测试](/software-testing.html)框架,用于测试基于 Java 的应用程序。 因此,在安装 JUnit 之前,您需要在计算机中配置或验证 Java 开发工具包(JDK)。 单击此[教程](/install-java.html)下载并安装 Java ## 第 2 部分)下载 JUnit **步骤 1)**访问 [http://junit.org/junit4/](http://junit.org/junit4/) ,然后点击下载并安装 ![How to Download and Installation JUnit](https://img.kancloud.cn/cc/01/cc01a9a2370a425193980f83b61c072e_699x399.png "How to Download and Installation JUnit") **步骤 2)**单击 junit.jar ![How to Download and Installation JUnit](https://img.kancloud.cn/80/4c/804c81e2dfc21536156c80edb11fe050_506x446.png "How to Download and Installation JUnit") **步骤 3)**在中央存储库中,显示了可以下载的所有 Junit 版本。 通常,您将选择最新版本。 单击 jar 链接以下载 Junit 版本 4.12,如下所示 ![How to Download and Installation JUnit](https://img.kancloud.cn/44/b7/44b7f6f41e856a78a9445b6b85d9a012_1009x413.png "How to Download and Installation JUnit") **步骤 4)**再次访问 [https://github.com/junit-team/junit4/wiki/下载并安装](https://github.com/junit-team/junit4/wiki/Download-and-Install)。 点击 hamcrest-core.jar ![How to Download and Installation JUnit](https://img.kancloud.cn/a8/0e/a80e7328f56c2232e3fc4e32c62dd8c4_512x192.png "How to Download and Installation JUnit") **步骤 5)**下载 Jar [![How to Download and Installation JUnit](https://img.kancloud.cn/f1/9c/f19c05567bc4479a7b18e62adf5d2870_838x132.png "How to Download and Installation JUnit") ](/images/junit/051716_0555_HowtoDownlo5.png) 对于 JUnit 安装,您需要 JUnit jar,并且可以从 JUnit 官方网站 [http://www.junit.org](https://junit.org/junit5/) 下载所需的 JUnit jar 文件版本。 这是罐子列表: * JUnit.jar * hamcrest-core.jar ## 第 3 部分)JUnit 环境设置 **步骤 1)**您需要设置 **JUNIT_HOME** 环境变量,以指出放置 JUnit Jar 的基本位置。 例如,如果您已经在 c:驱动器中创建了一个 JUnit 文件夹并将 jar 放在其中,那么对于环境设置,您需要打开控制面板->高级->环境变量。 1. 在环境窗口下,单击“新建”按钮。 ![How to Download and Installation JUnit](https://img.kancloud.cn/65/f4/65f4b45aa691abf4671e92e28ac6f631_394x436.png "How to Download and Installation JUnit") 当您在环境变量中单击新按钮时,它将打开另一个窗口 **步骤 2)**将打开“新系统变量”窗口: 1. 提供变量名称为“ JUNIT_HOME”。 2. 提供 JUnit 值作为已复制 JUnit jar 文件的 JUnit 路径。 3. 单击确定。 ![How to Download and Installation JUnit](https://img.kancloud.cn/05/af/05af07d79c1db81b583fe59d8b3bd6cc_364x156.png "How to Download and Installation JUnit") 当您单击确定时,它将使用给定的名称和值创建一个新的系统变量。 您可以在环境变量窗口中进行验证,如步骤 1 所示。 **步骤 3)**创建 JUNIT_HOME 之后,创建另一个名为 CLASSPATH 的变量。 再次转到环境变量,然后执行以下步骤。 1. Click on "new" button. When you click on new in environment variables, it will open another window. ![How to Download and Installation JUnit](https://img.kancloud.cn/39/ed/39ed0b617c9b4a9cdf2d7040801c7bf3_395x437.png "How to Download and Installation JUnit") **步骤 4)**在此步骤中,将 JUNIT_HOME 指向放置在 JUnit 文件夹中的 [JUnit.jar](https://bit.ly/My9IXz) ,如下所示: 1. 变量名称:CLASSPATH 2. 变量值:%CLASSPATH%;%JUNIT_HOME%\ JUnit4.10.jar;。; 3. 单击确定按钮。 ![How to Download and Installation JUnit](https://img.kancloud.cn/be/2b/be2bf814d97c016d764ccef188236d47_357x153.png "How to Download and Installation JUnit") **步骤 5)**单击“确定”按钮后,可以验证是否可以在系统变量下看到名为“ CLASSPATH”的新环境变量。 见下文 ![How to Download and Installation JUnit](https://img.kancloud.cn/d3/79/d379148f7465cd0b916872a1557a3730_394x436.png "How to Download and Installation JUnit") ## 第 4 部分)在 Eclipse 中安装 JUnit jar 文件 **步骤 1)**右键点击项目: 1. 点击“构建路径”,然后点击 2. 单击“配置构建路径”。 ![How to Download and Installation JUnit](https://img.kancloud.cn/01/33/013376aa00d9500310c4bc99ebbdd388_523x165.png "How to Download and Installation JUnit") **步骤 2)**在此步骤中, 1. 转到 Java 构建路径窗口,如下图所示 2. 现在,单击“添加外部 JAR”按钮以使用 Eclipse 添加下载的 JUnit.jar 文件。 添加 JUnit.jar 文件后,单击“确定”按钮以关闭 Java 构建路径窗口。 ![How to Download and Installation JUnit](https://img.kancloud.cn/9e/39/9e39c6d82b3a959cd7bdc7b634a7f85e_712x240.png "How to Download and Installation JUnit") ## 第 5 部分)验证 JUnit 所需的 jar 文件是否在我的构建路径中 为了在 Eclipse 中验证 JUnit jar 文件,您需要执行以下步骤: 1. 右键单击项目->构建路径 2. 单击“配置构建路径”。 ![How to Download and Installation JUnit](https://img.kancloud.cn/17/58/1758851c151db449e88aa8f194077f3e_528x175.png "How to Download and Installation JUnit") **步骤 2)** Java 构建路径窗口如下所示。 在该窗口中,转到“库”选项卡以查看所有 jar 文件。 在 jar 文件树视图中,您需要查找以 JUnit 开头的 jar 文件名。 扩展 JUnit 库后,您将看到如下所示的 Java 库: ![How to Download and Installation JUnit](https://img.kancloud.cn/8a/05/8a05145ebcb6795a0d263958169cf94f_294x213.png "How to Download and Installation JUnit") 现在,您准备将 JUnit 与 eclipse 一起使用。 ## 第 6 部分)验证 JUnit 设置 您可以创建一个简单的 JUnit 测试来验证 JUnit 设置。 参见下面的测试类: **步骤 1)**创建一个名为 TestJUnit.java 的 Java 类,并提供一个简单的 assert 语句。 ![How to Download and Installation JUnit](https://img.kancloud.cn/80/26/8026955d12cf3c3c734e1f19bc4fb89d_429x163.png "How to Download and Installation JUnit") ``` package guru99.junit; import org.junit.Test; import static org.junit.Assert.assertEquals; public class TestJunit { @Test public void testSetup() { String str= "I am done with Junit setup"; assertEquals("I am done with Junit setup",str); } } ``` **步骤 2)**创建一个 Test Runner 类以执行上述测试。 ![How to Download and Installation JUnit](https://img.kancloud.cn/9f/87/9f87d9b06a4f2e88ca1c3d10e008e864_476x205.png "How to Download and Installation JUnit") ``` package guru99.junit; import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(TestJunit.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println("Result=="+result.wasSuccessful()); } } ``` **步骤 3)**要对执行测试,请执行以下步骤: 1. 右键单击 TestRunner.java,然后单击“运行方式”,如下所示 2. 单击“运行方式”后,将打开另一个窗口,单击“ 1 JUnit Test”,如下所示: ![How to Download and Installation JUnit](https://img.kancloud.cn/79/22/792275a8b7e44cd26409f2bbf3135def_558x539.png "How to Download and Installation JUnit") **步骤 4)**这是测试的输出或结果。 如果执行成功,它将在其前面显示一个绿色的复选标记。 [![How to Download and Installation JUnit](https://img.kancloud.cn/02/55/02558397e83ff71a440e1736834c1fb9_335x401.png "How to Download and Installation JUnit") ](/images/junit/051716_0555_HowtoDownlo18.png) 单击以下载上面的代码以进行测试。