关于测试组件junit切换testng的示例以及切换方式分享

news/2024/5/8 3:05:56/文章来源:https://blog.csdn.net/vipshop_fin_dev/article/details/134108915

文章目录

    • 概要
    • 首先看看junit和testng的区别
    • 实践篇
      • 摸拟业务逻辑代码
        • 简单对象
        • 数据层摸拟类
        • 业务逻辑层摸拟类
        • 后台任务摸拟类
      • 基于spring+mock+junit
      • 基于spring+mock+testng
    • 示例的差异点
        • junit与testng的主要变动不大,有以下几个点需要注意
        • 注解部分
        • 在before,after中
        • testng多出按配置执行功能
        • 附上关于mock 新旧写法改进
    • 小结

概要

本文作者之前写单元测试都是使用junit
场景有以下三种场景
仅junit
spring+junit
mock+spring+junit

本文会用第三种场景写简单的实例列出junit和testng的代码相关说明
并会将涉及的修改点一一说明
目的帮助大家了解testng及具体的切换方式

首先看看junit和testng的区别

JUnit和TestNG是两种流行的Java测试框架,用于测试Java应用程序中的代码。它们具有以下区别:

  1. 组织方式:JUnit使用Annotations来标注测试方法,而TestNG使用XML文件来组织测试。

  2. 支持的测试类型:JUnit 4支持单元测试,而TestNG支持功能测试、集成测试和端到端测试。

  3. 并发测试:TestNG支持并发测试,可以在同一时间运行多个测试用例,而JUnit不支持并发测试。

  4. 数据提供者:TestNG支持数据提供者,可以在不同参数上运行相同的测试用例,而JUnit不支持数据提供者。

  5. 测试套件:TestNG支持测试套件,可以组织不同的测试用例,而JUnit不支持测试套件。

  6. 依赖测试:TestNG支持依赖测试,可以在一组测试之前运行必需的测试,而JUnit不支持依赖测试。

实践篇

摸拟业务逻辑代码

场景,有三层以上代码层次的业务场景,需要摸拟最底层数据层代码

简单对象
package com.riso.junit;/*** DemoEntity* @author jie01.zhu* date 2023/10/29*/
public class DemoEntity implements java.io.Serializable {private long id;private String name;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "DemoEntity{" + "id=" + id + ", name='" + name + '\'' + '}';}}
数据层摸拟类
package com.riso.junit;import org.springframework.stereotype.Component;/*** DemoDaoImpl* @author jie01.zhu* date 2023/10/29*/
@Component
public class DemoDaoImpl {public int insert(DemoEntity demoEntity) {System.out.println("dao.insert:" + demoEntity.toString());return 1;}
}
业务逻辑层摸拟类
package com.riso.junit;import org.springframework.stereotype.Service;import javax.annotation.Resource;/*** DemoServiceImpl* @author jie01.zhu* date 2023/10/29*/
@Service
public class DemoServiceImpl {@ResourceDemoDaoImpl demoDao;public int insert(DemoEntity demoEntity) {System.out.println("service.insert:" + demoEntity.toString());return demoDao.insert(demoEntity);}}
后台任务摸拟类
package com.riso.junit;import org.springframework.stereotype.Component;/*** DemoTaskImpl* @author jie01.zhu* date 2023/10/29*/
@Component
public class DemoTaskImpl {DemoServiceImpl demoService;public int insert(DemoEntity demoEntity) {System.out.println("task.insert:" + demoEntity.toString());return demoService.insert(demoEntity);}
}

基于spring+mock+junit

maven依赖

   <!-- test --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>3.12.4</version><scope>test</scope></dependency>
package com.riso.junit.test;import com.riso.junit.DemoDaoImpl;
import com.riso.junit.DemoEntity;
import com.riso.junit.DemoServiceImpl;
import com.riso.junit.DemoTaskImpl;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;/***  junit test* @author jie01.zhu* date 2023/10/29*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"}, inheritLocations = true)
public class Test1 {/*** 测试入口类*/@Resource@InjectMocksDemoTaskImpl demoTask;/*** mock的类的中间传递类*/@Resource@InjectMocksDemoServiceImpl demoService;/*** 被mock的类*/@MockDemoDaoImpl demoDao;@Testpublic void test1() {// 初始化mock环境MockitoAnnotations.openMocks(this);DemoEntity demoEntity = new DemoEntity();demoEntity.setId(1L);demoEntity.setName("name1");Mockito.doReturn(0).when(demoDao).insert(Mockito.any());int result = demoTask.insert(demoEntity);Assert.assertEquals(result, 0);}
}

基于spring+mock+testng

有二个测试类,测试参数不同,主要体现在单元测试外,控制二个测试类,按并发场景做简单的集成测试

maven依赖

        <dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>6.14.3</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>2.4.13</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>3.12.4</version><scope>test</scope></dependency>
package com.riso.testng.test;import com.riso.testng.ContextConfig;
import com.riso.testng.DemoDaoImpl;
import com.riso.testng.DemoEntity;
import com.riso.testng.DemoTaskImpl;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;import javax.annotation.Resource;/***  junit test* @author jie01.zhu* date 2023/10/29*/
@SpringBootTest(classes = {ContextConfig.class})
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class Test1 extends AbstractTestNGSpringContextTests {/*** 测试入口类*/@ResourceDemoTaskImpl demoTask;/*** 被mock的类 选用spy方式  默认使用原生逻辑,仅mock的方法才被mock*/@SpyBeanDemoDaoImpl demoDao;@Testpublic void test1() {// 初始化mock环境MockitoAnnotations.openMocks(this);DemoEntity demoEntity = new DemoEntity();demoEntity.setId(1L);demoEntity.setName("name1");Mockito.doReturn(0).when(demoDao).insert(Mockito.any());int result = demoTask.insert(demoEntity);Assert.assertEquals(result, 0);}
}package com.riso.testng.test;import com.riso.testng.ContextConfig;
import com.riso.testng.DemoDaoImpl;
import com.riso.testng.DemoEntity;
import com.riso.testng.DemoTaskImpl;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;import javax.annotation.Resource;/***  junit test* @author jie01.zhu* date 2023/10/29*/
@SpringBootTest(classes = {ContextConfig.class})
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class Test2 extends AbstractTestNGSpringContextTests {/*** 测试入口类*/@ResourceDemoTaskImpl demoTask;/*** 被mock的类 选用spy方式  默认使用原生逻辑,仅mock的方法才被mock*/@SpyBeanDemoDaoImpl demoDao;@Testpublic void test2() {// 初始化mock环境MockitoAnnotations.openMocks(this);DemoEntity demoEntity = new DemoEntity();demoEntity.setId(2L);demoEntity.setName("name2");Mockito.doReturn(2).when(demoDao).insert(Mockito.any());int result = demoTask.insert(demoEntity);Assert.assertEquals(result, 2);}
}

testNg的 配置文件,也是执行入口

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test" parallel="tests" thread-count="2"><test name="test1" group-by-instances="true"><classes><class name="com.riso.testng.test.Test1"/></classes></test><test name="test2" group-by-instances="true"><classes><class name="com.riso.testng.test.Test2"></class></classes></test>
</suite>

运行方式如下:
在这里插入图片描述

示例的差异点

junit与testng的主要变动不大,有以下几个点需要注意
注解部分

junit此处注解
@RunWith(SpringJUnit4ClassRunner.class)
testng不再使用此注解
需要继承 org.springframework.test.context.testng.AbstractTestNGSpringContextTests

在before,after中

testng完全兼容,但会多出Suite ,它代替xml配置中,单元测试类之上的生命周期

testng多出按配置执行功能

首先testng的单元测试可以与junit一样,单独运行
在这个基础上,也能通过testng xml按配置运行,可以见上面的例子

附上关于mock 新旧写法改进

以前要摸拟调用对象的跨二层以上类时,需要通过InjectMocks 做为中间传递,才能成功mock掉二层以上的类
换成spyBean后,不需要再使用InjectMocks ,会自动从注入中找到
这个小插曲也是我自己对以前mock的修正,一并附上

小结

通过以上说明,及示例
testng是完全兼容junit的,且改动很小
注解,断言都是直接兼容的(只需要更换导入的包路径既可)

当然,我不是为了使用而使用,一切都要建立上有需求的基础上
junit对我来讲,已经满足不了我的需求,
为了能够编写集成测试,同时复用已有的单元测试,我选择了testng
希望以上分享,能够对读者有用.

朱杰
2023-10-29

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_190559.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

3 — NLP 中的标记化:分解文本数据的艺术

一、说明 这是一个系列文章的第三篇文章&#xff0c; 文章前半部分分别是&#xff1a; 1 、NLP 的文本预处理技术 2、NLP文本预处理技术&#xff1a;词干提取和词形还原 在本文中&#xff0c;我们将介绍标记化主题。在开始之前&#xff0c;我建议您阅读我之前介绍的关…

Http代理与socks5代理有何区别?如何选择?(一)

了解SOCKS和HTTP代理之间的区别对于优化您的在线活动至关重要&#xff0c;无论您是技术娴熟的个人、现代互联网用户还是企业所有者。在使用代理IP时&#xff0c;您需要先了解这两种协议之间的不同。 一、了解HTTP代理 HTTP&#xff08;超文本传输协议&#xff09;代理专门设计…

【三方登录-Apple】iOS 苹果授权登录(sign in with Apple)之开发者配置一

记录一下sign in with Apple的开发者配置 前言 关于使用 Apple 登录 使用“通过 Apple 登录”可让用户设置帐户并使用其Apple ID登录您的应用程序和关联网站。首先使用“使用 Apple 登录”功能启用应用程序的App ID 。 如果您是首次启用应用程序 ID 或为新应用程序启用应用程序…

猫头虎为不同行业精心挑选的MacBook Pro配置指南之深度解析:如何根据行业需求精准选择MacBook Pro配置 - M1, M2, M3系列全面对比

&#x1f337;&#x1f341; 博主猫头虎 带您 Go to New World.✨&#x1f341; &#x1f984; 博客首页——猫头虎的博客&#x1f390; &#x1f433;《面试题大全专栏》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33a; &a…

[Unity][VR]透视开发系列4-解决只看得到Passthrough但看不到Unity对象的问题

【视频资源】 视频讲解地址请关注我的B站。 专栏后期会有一些不公开的高阶实战内容或是更细节的指导内容。 B站地址: https://www.bilibili.com/video/BV1Zg4y1w7fZ/ 我还有一些免费和收费课程在网易云课堂(大徐VR课堂): https://study.163.com/provider/480000002282025/…

Mybatis—XML配置文件、动态SQL

学习完Mybatis的基本操作之后&#xff0c;继续学习Mybatis—XML配置文件、动态SQL。 目录 Mybatis的XML配置文件XML配置文件规范XML配置文件实现MybatisX的使用 Mybatis动态SQL动态SQL-if条件查询 \<if\>与\<where\>更新员工 \<set\>小结 动态SQL-\<forea…

java基础+数据库基础+系统+JVM问题

的哎的哎 1、基础部分 java线程池 队列的选择 答&#xff1a; SingleThreadPool:适用于多个任务顺序执行的场景。 它使用的是LinkedBlockingQueue<>()&#xff0c;无界的阻塞队列&#xff0c;就意味着会有内存溢出的风险。 FixedThreadPool: 适用于任务量固定耗时长的…

线段树 区间赋值 + 区间加减 + 求区间最值

线段树好题&#xff1a;P1253 扶苏的问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 区间赋值 区间加减 求区间最大。 对于区间赋值和区间加减来说&#xff0c;需要两个懒标记&#xff0c;一个表示赋值cover&#xff0c;一个表示加减add。 区间赋值的优先级大于区间加…

软件测试之接口测试详解

首先&#xff0c;什么是接口呢&#xff1f; 接口一般来说有两种&#xff0c;一种是程序内部的接口&#xff0c;一种是系统对外的接口。 系统对外的接口&#xff1a;比如你要从别的网站或服务器上获取资源或信息&#xff0c;别人肯定不会把数据库共享给你&#xff0c;他只能给…

Flask-SQLAlchemy事件钩子介绍

一、前言 前几天在搜资料的时候无意中看到有介绍SQLAlchemy触发器&#xff0c;当时感觉挺奇怪的&#xff0c;触发器不是数据库层面的概念吗&#xff0c;怎么flask-SQLAlchemy这个ORM框架会有这玩意。 二、SQLAlchemy触发器一个简单例子 考虑到效率博客表中有两个字段&#xf…

在Qt中List View和List Widget的区别是什么,以及如何使用它们

2023年10月29日&#xff0c;周日晚上 目录 List View和List Widget的区别 如何使用QListView 如何使用QListWidget List View和List Widget的区别 在Qt中&#xff0c;QListView 和 QListWidget 是用于显示列表数据的两个常用控件&#xff0c;它们有一些区别和特点。 1. 数…

​学习一下,什么是预包装食品?​

预包装食品&#xff0c;指预先定量包装或者制作在包装材料和容器中的食品&#xff1b;包括预先定量包装以及预先定量制作在包装材质和容器中并且在一定量限范围内具有统一的质量或体积标识的食品。简单说&#xff0c; 就是指在包装完成后即具有确定的量值&#xff0c;这一确定的…

第五章 I/O管理 六、I/O核心子系统

目录 一、核心子系统 1、I/O调度 2、设备保护 二、假脱机技术 1、脱机&#xff1a; 2、假脱机&#xff08;SPOOLing技术&#xff09;&#xff1a; 3、应用&#xff1a; 1.独占式设备&#xff1a; 2.共享设备&#xff1a; 4、共享打印机原理分析 三、总结 一、核心子系…

新恶意软件使用 MSIX 软件包来感染 Windows

人们发现&#xff0c;一种新的网络攻击活动正在使用 MSIX&#xff08;一种 Windows 应用程序打包格式&#xff09;来感染 Windows PC&#xff0c;并通过将隐秘的恶意软件加载程序放入受害者的 PC 中来逃避检测。 Elastic Security Labs 的研究人员发现&#xff0c;开发人员通常…

vscode debug skills

1) VSCode 调试 C/C 代码时&#xff0c;如何显示动态分配的指针数组。 创建一个动态分配的一维数组: int n 10; int *array (int *)malloc(n*sizeof(int)); memset(array, 1, n*sizeof(int)); 如果直接 Debug 时查看 array 指针&#xff0c;并不能看到数组所有的值。 查看…

Linux启动之uboot分析

Linux启动之uboot分析 uboot是什么&#xff1f;一、补充存储器概念1.存储器种类1.norflash - 是非易失性存储器&#xff08;也就是掉电保存&#xff09;2.nandflash - 是非易失性存储器&#xff08;也就是掉电保存&#xff09;3.SRAM - 静态随机访问存储器 - Static Random Acc…

用友 GRP-U8 存在sql注入漏洞复现

0x01 漏洞介绍 用友 GRP-U8 license_check.jsp 存在sql注入&#xff0c;攻击者可利用该漏洞执行任意SQL语句&#xff0c;如查询数据、下载数据、写入webshell、执行系统命令以及绕过登录限制等。 fofa&#xff1a;app”用友-GRP-U8” 0x02 POC: /u8qx/license_check.jsp?kj…

python采集电商jd app搜索商品数据(2023-10-30)

一、技术要点&#xff1a; 1、cookie可以从手机app端用charles抓包获取&#xff1b; 2、无需安装nodejs&#xff0c;纯python源码&#xff1b; 3、搜索接口为&#xff1a;functionIdsearch&#xff1b; 4、clientVersion "10.1.4"同时也支持更高的版本&#xff1b; …

SpringBoot,使用JavaMailSender发送邮件(含源码)。

本文主要讲解使用JavaMailSender发送邮件&#xff0c;并给出对应的参考案例、源码。 1、使用的依赖jar包 JavaMailSender发送邮件&#xff0c;只需要 "spring-boot-starter-mail" jar包就可以。考虑到邮件发送时&#xff0c;使用 Hutool工具生成Excel文件做为附件&am…

“道法自然——徐铭中国画展”在中国美术馆隆重开幕

10月28日上午&#xff0c;“道法自然——徐铭中国画作品展”在中国美术馆隆重开幕。本次展览由中国科学院大学、民盟中央美术院联合主办。第十四届全国人大常委会委员、财政经济委员会副主任委员、民盟中央副主席谢经荣&#xff0c;第十四届全国政协副秘书长、民盟中央副主席、…