JUnit Test Cases are normal java class with methods to be executed by JUnit framework. JUnit 5 is a major upgrade from earlier versions.

JUnit测试用例是普通的Java类,带有要由JUnit框架执行的方法。 JUnit 5是早期版本的主要升级。

在Eclipse中创建JUnit测试用例 (Creating JUnit Test Cases in Eclipse)

Latest Eclipse versions come with built-in support for JUnit test cases creation. We can easily create JUnit test cases class in Eclipse as shown below.

最新的Eclipse版本附带了对JUnit测试用例创建的内置支持。 我们可以在Eclipse中轻松创建JUnit测试用例类,如下所示。

Go to New | JUnit Test Case in your Eclipse.

转到新| Eclipse中的JUnit测试用例

In the next popup window, provide the test class name, its package, method stubs to generate etc. Note that Eclipse provides us option to create JUnit 3, JUnit 4 and JUnit Jupiter Test Cases. Unless your project is using the older version of JUnit and you haven’t migrated to the latest versions, it’s better to create the JUnit Jupiter test case.

在下一个弹出窗口中,提供测试类名称,其包,要生成的方法存根等。请注意,Eclipse提供了创建JUnit 3,JUnit 4和JUnit Jupiter测试用例的选项。 除非您的项目使用的是JUnit的较早版本,并且尚未迁移到最新版本,否则最好创建JUnit Jupiter测试用例。

Once you click on “Finish” button, the test case class will be generated. You can run this class by going to Run | Run As | JUnit Test. You can also reach this menu by right-clicking in the Editor window or by selecting the class and then right click on it.

单击“完成”按钮后,将生成测试用例类。 您可以通过运行|运行该类 运行方式| JUnit测试 。 您也可以通过在“编辑器”窗口中右键单击或选择该类,然后在其上单击鼠标右键来访问此菜单。

JUnit Eclipse运行配置 (JUnit Eclipse Run Configurations)

You can also create run configurations to execute JUnit test cases. Run configurations are helpful in running multiple test classes at once by selecting the package, or selecting only a few methods to run in the test class.

您还可以创建运行配置以执行JUnit测试用例。 通过选择程序包或仅选择一些方法在测试类中运行,运行配置有助于一次运行多个测试类。

When you run a JUnit test, a sample run configuration is automatically created. So you can just edit them as per your requirements.

运行JUnit测试时,将自动创建一个示例运行配置。 因此,您可以根据需要进行编辑。

使用Maven Build执行JUnit测试用例 (JUnit Test Cases Execution with Maven Build)

If you want your JUnit 5 test cases to be executed with maven build, you will have to configure maven-surefire-plugin with junit-platform-surefire-provider dependencies.

如果要使用Maven构建执行JUnit 5测试用例,则必须使用junit-platform-surefire-provider依赖项配置maven-surefire-plugin

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.22.0</version><dependencies><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-surefire-provider</artifactId><version>1.2.0</version></dependency></dependencies></plugin></plugins>
</build>

Here is a simple JUnit test class I have created in my example project.

这是我在示例项目中创建的简单JUnit测试类。

package com.journaldev.annotations;import static org.junit.jupiter.api.Assertions.*;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;import com.journaldev.utils.MyUtils;class MyUtilsTest {@BeforeAllstatic void setUpBeforeClass() throws Exception {System.out.println("Set Up Before Class - @BeforeAll");}@AfterAllstatic void tearDownAfterClass() throws Exception {System.out.println("Tear Down After Class - @AfterAll");}@BeforeEachvoid setUp() throws Exception {System.out.println("Set Up @BeforeEach");}@AfterEachvoid tearDown() throws Exception {System.out.println("Tear Down @AfterEach");}@Testvoid test_add() {assertEquals(10, MyUtils.add(5, 5));}@Testvoid test_reverse() {assertEquals("cba", MyUtils.reverse("abc"));}
}

When the mvn test command is executed, it produces the following snippet for our JUnit test class execution.

当执行mvn test命令时,它将为我们的JUnit测试类执行生成以下代码段。

[INFO] Running com.journaldev.annotations.MyUtilsTest
Set Up Before Class - @BeforeAll
Set Up @BeforeEach
Tear Down @AfterEach
Set Up @BeforeEach
Tear Down @AfterEach
Tear Down After Class - @AfterAll
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.journaldev.annotations.MyUtilsTest

If you want to skip test cases execution, you can run the following command:

如果要跳过测试用例的执行,可以运行以下命令:

mvn clean install -Dmaven.test.skip=true

摘要 (Summary)

JUnit test cases help us in unit testing our code. Eclipse IDE provides great built-in support for JUnit test classes creation and execution. We also learned how to configure our maven project to execute JUnit Jupiter test cases from command line maven build.

JUnit测试用例可帮助我们对代码进行单元测试。 Eclipse IDE为JUnit测试类的创建和执行提供了强大的内置支持。 我们还学习了如何从命令行maven构建中配置maven项目以执行JUnit Jupiter测试用例。

GitHub Repository.GitHub Repository中查看带有更多示例的JUnit示例项目。

翻译自: https://www.journaldev.com/21621/junit-test-cases-eclipse-maven

JUnit测试用例– Eclipse和Maven相关推荐

  1. 今天重新建立了个eclipse 的maven项目,提示org.junit找不到

    今天重新建立了个eclipse 的maven项目,TEST包中的代码提示org.junit找不到 明明查看到library中的maven dependencies中有对应的junit  jar包 将光 ...

  2. Eclipse中Maven常用命令以及Maven生命周期详解

    Eclipse中maven常用的命令 在某一个maven项目右键快捷方式,然后点击Run As就可以发现几个Maven的命令: Maven Build: 这个命令用于编译Maven工程,执行命令后会在 ...

  3. eclipse 创建maven 项目 动态web工程完整示例

    需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的eclipse等已经集成配置好了maven了,说白了就是新建项目的时候已经可以找到maven了 没有的话需要安装m ...

  4. [Eclipse的Maven项目搭建,仅为测试Maven功能]如何在Eclipse下搭建Maven项目

    你可能需要了解以下才能更好的阅读以下: 在 Windows 中配置Maven: http://www.cnblogs.com/chanchifeng/p/6195149.html 在新版本的eclip ...

  5. 使用Eclipse构建Maven的SpringMVC项目

    http://limingnihao.iteye.com/blog/830409 使用Eclipse构建Maven的SpringMVC项目 首先Eclipse需要安装Maven的插件,地址:http: ...

  6. resteasy_Tomcat 7上具有RESTeasy JAX-RS的RESTful Web服务– Eclipse和Maven项目

    resteasy 开发Web服务的RESTful方法不断受到越来越多的关注,并且似乎正在将SOAP淘汰. 我不会讨论哪种方法更好,但是我相信我们都同意REST更轻量级. 在本教程中,我将向您展示如何使 ...

  7. Tomcat 7上具有RESTeasy JAX-RS的RESTful Web服务-Eclipse和Maven项目

    开发Web服务的RESTful方法不断受到越来越多的关注,并且似乎正在将SOAP淘汰. 我不会讨论哪种方法更好,但是我相信我们都同意REST更轻量级. 在本教程中,我将向您展示如何使用RESTeasy ...

  8. maven 学习笔记(三)创建一个较复杂的 eclipse+android+maven 工程

    前面maven 学习笔记(二)已经说过了怎样通过插件创建一个简单的工程,有了前面的基础,创建一个较复杂的工程就容易了很多.同样是通过已经有了插件,同样如果插件系统中并未存在,还是需要通过Add Arc ...

  9. eclipse 使用maven 创建springmvc + mybatis

    接着eclipse 使用maven 创建纯spring mvc项目 毕竟项目都要访问数据库的, 所以加上mybatis的支持也就是网上大多时候说的 SSM框架的搭建(Spring + Spring M ...

最新文章

  1. 3月31日华为鸿蒙,华为鸿蒙OS Beta 3将从3月31日起推送
  2. Flutter开发环境安装
  3. ML之Hash_EditDistance:基于输入图片哈希化(均值哈希+差值哈希)即8*8个元素的单向vector利用编辑距离算法进行判别
  4. php 重复区域,如何使用Mysql和PHP从重复区域单击缩略图后检索图像
  5. CentOS7部署NFS
  6. C# 使用int.TryParse,Convert.ToInt32,(int)将浮点类型转换整数时的区别
  7. Linux进程全解2——进程环境(环境变量、进程运行的虚拟地址空间)
  8. HTTP Session 的工作原理以及几个思维扩展
  9. rocketmq同步消息,异步消息
  10. android scrollview 动态添加,使用Scrollview和LinearLayout动态添加布局
  11. Atitit 人工智能体系树完整版 Atitit 人工智能体系培训列表 目录 1. 1.NLP自然语言处理文本处理 1 2. 知识图谱 知识处理系统 2 3. 2.机器视觉 图像处理 2 4.
  12. UG命令大全及快捷键的用法用处说明
  13. 数据结构与算法分析:算法分析
  14. 《R语言与数据挖掘》⑥-④分类与预测建模【KNN算法】
  15. 中冠百年|家庭理财投资必备资金规划有哪些
  16. android-b9是什么设备,Android 设备上可以实现 3D Touch 吗?| 原力计划
  17. 西西吹雪:从程序员到项目经理(二)
  18. 关于AD导出PDF文件出现问题汇总
  19. 【HTML+CSS(六)】
  20. 生物信息学 | 富集分析

热门文章

  1. Activity与Intent机制的学习笔记--转自feisky
  2. 不支持对系统目录进行即席更新
  3. [转载] python json unicode utf-8处理总结
  4. [转载] python 语言基础 - 字符串常用函数及操作
  5. 编程没点为什么,生活就是十万个为什么
  6. 返回表对象的方法之一--bulk collect into
  7. 排序算法之------归并排序
  8. 对象functionJavaScript: The Definitive Guide 权威指南,读书笔记(一)
  9. 基本完成的重力空间的对任意形状的碰撞子系统
  10. C#连接SQL Server数据库