当前位置:网站首页>SSM framework series - JUnit unit test optimization day2-3
SSM framework series - JUnit unit test optimization day2-3
2022-04-23 12:42:00 【Concise programming】
SSM Frame series ——Junit Unit test optimization
Junit
brief introduction (from baidu)
JUnit It's a Java The language's unit testing framework . It consists of Kent Beck and Erich Gamma establish , Gradually become from Kent Beck Of sUnit Of xUnit The most successful one in the family . JUnit It has its own JUnit Expand the ecosystem . Most of the Java The development environment of has been integrated JUnit As a tool for unit testing
JUnit By Erich Gamma and Kent Beck Write a regression testing framework (regression testing framework).Junit Testing is programmer testing , The so-called white box test , Because programmers know how the software being tested (How) How to complete the function and what to complete (What) The function of .Junit It's a framework , Inherit TestCase class , You can use it Junit It's automated
Junit5 Can't use @RunWith
If you used the previous Spring In the version, everyone is doing junit Unit tests will be used @RunWith Appoint Spring Kernel testing , however Spring5 After use junit There is no such annotation
reason
It's actually Spring Support Junit5, stay 5 There is no such annotation in. It has been integrated, so it doesn't need to be used anymore @RunWith()
@RunWith The functions of are @ExtendWith replace , The original @RunWith(SpringRunner.class) By the same function @ExtendWith(SpringExtension.class) replace . but JUnit5 in @SpringBootTest The annotation already contains... By default @ExtendWith(SpringExtension.class).
So Spring Can only be used in Spring4
Because the version corresponds to @ExtendWith(SpringJUnit4ClassRunner.class)
I won't write code for you here
See my article on unit testing :SpringBoot—— unit testing
But if you reduce the version to use Junit4 To test, I give the following simple example
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;
// Specifies the kernel to use
@RunWith(SpringJUnit4ClassRunner.class)
//@ExtendWith(SpringJUnit4ClassRunner.class)
// Indicates the configuration context
@ContextConfiguration(classes = {
TestConfiguation.class})
public class TestBetter {
// Automatic injection
@Autowired
private TestService testService;
@Test
public void testServiceTest(){
this.testService.testShow();
}
}

版权声明
本文为[Concise programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231239441816.html
边栏推荐
猜你喜欢

XinChaCha Trust SSL Organization Validated

万事有你 未来可期 | ONES 2022校园招聘正式开启

Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward

Qt进程间通信

Realize several "Postures" in which a box is horizontally and vertically centered in the parent box

网络信息安全之零信任

Object. The disorder of key value array after keys

Remote sensing image classification and recognition system based on convolutional neural network

Sort out several uses of network IP agent

Introduction to kubernetes
随机推荐
box-sizing
QT draw text
Kubernets Getting started tutoriel
NBIOT的AT指令
Pre competition practice of TIANTI competition
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
Qt绘制文字
Jiachen chapter Genesis "inner universe" joint Edition
一个平面设计师的异想世界|ONES 人物
RT-thread中关键词解释及部分API
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
Qt重绘事件与剪切
ZigBee CC2530 minimum system and register configuration (1)
Lesson 26 static member functions of classes
解锁OpenHarmony技术日!年度盛会,即将揭幕!
Resolve disagrees about version of symbol device_ create
Kubernetes 入門教程
消息队列概述
PHP generates JSON to process Chinese
传统企业如何应对数字化转型?这些书给你答案