从 SpringBoot 2.2.0 之后,Junit5 已经成为了默认的 Junit 版本。有了 JUnit Vintage,从 Junit4 迁移到 Junit5 的成本极低。所以本文就直接针对 Junit5 开始了。
版本
–
先说版本,是为了避免因为版本差异出现各种奇怪的问题:
-
JDK:jdk8(小版本可以忽略)
-
SpringBoot:2.5.2
-
继承
spring-boot-starter-parent
-
依赖
spring-boot-starter-web
-
依赖
spring-boot-starter-test
-
JUnit:5.7.2
-
Mockito:3.9.0
-
hamcrest:2.2
SpringBoot 的好处在于,只要继承spring-boot-starter-parent
或引入spring-boot-dependencies
,然后添加spring-boot-starter-test
依赖即可。定义的 POM 内容如下:
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.2
cn.howardliu.effective.spring
springboot-junit5-mockito
0.0.1-SNAPSHOT
springboot-junit5-mockio
org.springframework.boot