目录

(二)JUnit 安装

Junit目前分两个版本,Junit4 和 Junit5 , 本系列教程打算从 Junit4 开始介绍,最后,再介绍 Junit5 有哪些新特性。

IntelliJ IDEA 安装 Junit


Java 开发的同学,推荐使用 IntelliJ IDEA,推荐阅读《IntelliJ IDEA 教程》。

1、下载 junit-4.12.jar 文件:https://github.com/junit-team/junit4/releases

2、 打开 IntelliJ IDEA ,菜单栏:File菜单 –> Porject Structure 选项 –> Dependencies 标签 –> 点击 “+” 号 –> Library… –> Java 。 选择下载的 junit-4.12.jar 进行添加。

http://img.testclass.net/junit_add_jar.png

3、以同样的方式下载和导入 hamcresthttps://github.com/hamcrest/JavaHamcrest/releases ,否则,你将无法运行 Junit 单元测试。

Maven 安装 Junit


相比较而言,Maven 的安装要简单很多,打开你 Maven 项目中的 pom.xml 文件,添加如下配置:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

更多的 Maven 项目,可以登录:https://www.mvnrepository.com 网站查找。

原始封面

https://images.unsplash.com/photo-1482062364825-616fd23b8fc1?w=300