当前位置:网站首页>SSM框架系列——Junit单元测试优化day2-3
SSM框架系列——Junit单元测试优化day2-3
2022-04-23 12:39:00 【简明编程】
SSM框架系列——Junit单元测试优化
Junit
简介(from baidu)
JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具
JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了
Junit5无法使用@RunWith
如果用过之前的Spring版本的话大家在做junit单元测试的时候会使用@RunWith指定Spring内核进行测试,但是Spring5后使用junit已经没有了这个注解
原因
其实是Spring支持Junit5,在5中没有这个注解了已经被集成进去了所以不需要再用@RunWith()
@RunWith 的功能都被 @ExtendWith 替代,即原 @RunWith(SpringRunner.class) 被同功能的 @ExtendWith(SpringExtension.class) 替代。但 JUnit5 中 @SpringBootTest 注解中已经默认包含了 @ExtendWith(SpringExtension.class)。
所以说Spring中只能用Spring4
因为版本对应@ExtendWith(SpringJUnit4ClassRunner.class)
这里我就不给大家写代码了
有关单元测试的看我这篇:SpringBoot——单元测试
但如果你降低版本使用Junit4进行测试我给出以下测试简单示例
package com.exampl.junitTest;
import com.exampl.configuration.TestConfiguation;
import com.exampl.service.TestService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//指定使用内核
@RunWith(SpringJUnit4ClassRunner.class)
//@ExtendWith(SpringJUnit4ClassRunner.class)
//指明配置上下文
@ContextConfiguration(classes = {
TestConfiguation.class})
public class TestBetter {
//自动注入
@Autowired
private TestService testService;
@Test
public void testServiceTest(){
this.testService.testShow();
}
}

版权声明
本文为[简明编程]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51553982/article/details/124358731
边栏推荐
- 为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
- Message queuing overview
- BUUCTF WEB [BJDCTF2020]The mystery of ip
- BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
- XinChaCha Trust SSL Organization Validated
- 大家帮我看一下这是啥情况,MySQL5.5的。谢了
- Unlock openharmony technology day! The annual event is about to open!
- [redis series] redis learning 13. Redis often asks simple interview questions
- Stm32cubeprogrammer basic instructions
- ZigBee CC2530 minimum system and register configuration (1)
猜你喜欢

AI video cloud vs narrowband HD, who is the darling of the video era

Introduction to metalama 4 Use fabric to manipulate items or namespaces

一个平面设计师的异想世界|ONES 人物

电脑系统卡如何解决?

力扣刷题之完全二叉树的节点个数

Recommended programming AIDS: picture tool snipaste

Kubernetes 入门教程

传统企业如何应对数字化转型?这些书给你答案

解决disagrees about version of symbol device_create

S2-062 远程命令执行漏洞复现(cve-2021-31805)
随机推荐
C, calculation code of parameter points of two-dimensional Bezier curve
天梯赛赛前练习
leetcode:437. Path sum III [DFS selected or not selected?]
消息队列概述
mysql中 innoDB执行过程分析
Unlock openharmony technology day! The annual event is about to open!
大家帮我看一下这是啥情况,MySQL5.5的。谢了
Qt双缓冲绘图
Qt进程间通信
S2-062 远程命令执行漏洞复现(cve-2021-31805)
SQLserver怎么插入或更新当天的星期数,bit而不是文本
RT-thread中关键词解释及部分API
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
[unity note] basic lighting in l4unity
A graphic designer's fantasy world | ones characters
Aviation core technology sharing | overview of safety characteristics of acm32 MCU
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
QT redraw events and cuts
box-sizing
Introduction to metalama 4 Use fabric to manipulate items or namespaces