目录
1.环境
springboot2.4.0
Junit5
如果想改变spirngboot版本可修改2.4.0位置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.写测试类的位置
在src-test文件夹下写测试类,打包部署时不会被编译
3.pom.xml中导入相关依赖依赖
springboot2.4.0 以上版本移除了默认对Vintage的依赖。如果需要兼容junit4需要自行引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
如需使用测试套件,需引入一下依赖
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
参数化的依赖
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
4.Junit5测试类最基础模板
(1)Junit4 需要加@Runwith注解
Junit5 不用加@Runwith注解:@ExtendWith注解代替了 @Runwith,而 @SpringBootTest 注解已经定义了 @ExtendWith的默认值
(2)@Test注解:标记方法为测试方法
package com.example;
//import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringbootDemoApplicationTests {
@Test
public void a(){
System.out.println ("a");
}
}
5.注解(@BeforeEach、@BeforeAll、@AfterEach、@AfterAll)
(1)@BeforeEach:同一个类中每个测试方法执行前都会执行一次
package com.example;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class TestDemo1 {
@BeforeEach
void beforEachInit(){
System.out.println("111111");
}
@Test
public void a(){
System.out.print("aa");
}
@Test
public void b(){
System.out.print("bb");
}
@Test
public void c(){
System.out.print("cc");
}
}
(2)@BeforeAll:同一个类中所有测试方法执行之前执行一次,且被该注解修饰的方法必须为静态方法
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
//@SpringBootTest
public class TestDemo1 {
@BeforeEach
public void beforEachInit(){
System.out.println("beforEach");
}
@BeforeAll
public static void beforeAllInit(){
System.out.println("beforeAll");
}
@Test
public void a(){
System.out.print("aa");
}
@Test
public void b(){
System.out.print("bb");
}
@Test
public void c(){
System.out.print("cc");
}
}
(3)@AfterEach:同一个类中每个测试方法执行之后都执行一次
(4)@AfterAll:同一个类中所有用例执行之后执行一次,且被该注解修饰的方法必须为静态方法
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;
//@SpringBootTest
public class TestDemo1 {
@BeforeEach
public void beforEachInit(){
System.out.println("beforEach");
}
@BeforeAll
public static void beforeAllInit(){
System.out.println("beforeAll");
}
@Test
public void a(){
System.out.println("aa");
}
@Test
public void b(){
System.out.println("bb");
}
@AfterEach
public void afterEach(){
System.out.println("afterEach");
}
@AfterAll
public static void afterAll(){
System.out.println("afterAll");
}
}
6.测试套件
概念:用来组织和运营一组相关的测试
(创建一个测试套件类,里面指定好可以执行哪些测试类。只启动这个套件类,指定的类中的方法便可执行)
测试套件类TotalDemo(配置了TestDemo2、TestDemo3测试方法)
package com.example;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SelectClasses({TestDemo2.class, TestDemo3.class})
public class TotalDemo {
}
TestDemo2
package com.example;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
//@SpringBootTest
public class TestDemo2 {
@Test
public void testdemo2(){
System.out.println("这是TestDemo2的方法");
}
}
TestDemo3
package com.example;
import org.junit.jupiter.api.Test;
public class TestDemo3 {
@Test
public void testdemo3(){
System.out.println("这是TestDemo3的方法");
}
}
执行测试套件类TotalDemo