Spring Boot整合Junit 5

目录

  • Spring Boot整合Junit 5
    • 环境介绍
    • 搭建一个maven工程
    • 编写一个单元测试的父类
    • 开始写单元测试
    • 常用注解
      • @BeforeAll
      • @BeforeEach
      • @AfterEach
      • @AfterAll
      • @DisplayName
      • @TestPropertySource
      • @Autowired
      • @Qualifier

Spring Boot整合Junit 5

要求读者对Spring Boot有基本的了解,本文不再对Spring Boot做基本介绍。

本文主要介绍Spring BootJunit整合,实现单元测试。

环境介绍

软件名称 软件版本
Spring Boot 2.5.3
Maven 3.6.3

搭建一个maven工程

修改pom.xml,指定父级依赖


	org.springframework.boot
	spring-boot-starter-parent
	2.5.3

编写一个单元测试的父类

/**
 * 单元测试可以继承此类。
 * 
 * @author Etomy
 */
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractSpringbootTestBase extends Assertions {
	
}

开始写单元测试

package com.etomy.teach.junit;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AnnotationTest extends AbstractSpringbootTestBase {
	
	private static final String LOGGER_TEST = "==== {} ====";

	// @BeforeAll 类似于JUnit 4的@BeforeAll,表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前执行,必须为static
	@BeforeAll
	public static void beforeAll() {
		log.debug(LOGGER_TEST, 1);
	}
	
	// @BeforeEach 类似于JUnit 4的@Before,表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前执行
	@BeforeEach
	public void beforeEach() {
		log.debug(LOGGER_TEST, 2);
	}
	
	// @AfterEach 类似于JUnit 4的@After,表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行。
	@AfterEach 
	public void afterEach () {
		log.debug(LOGGER_TEST, 3);
	}
		
	// @AfterAll 类似于JUnit 4的@AfterClass,表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行,必须为static
	@AfterAll 
	public static void afterAll () {
		log.debug(LOGGER_TEST, 4);
	}

	// @DisplayName 为测试类或测试方法声明一个自定义的显示名称。
	@DisplayName("ttttttttt")
	// @Test 表示该方法是一个测试方法。
	@Test
	public void test() {
		log.debug(LOGGER_TEST, "hello world");
	}
}

常用注解

@BeforeAll

类似于JUnit 4的@BeforeAll,表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前执行,必须为static

@BeforeEach

类似于JUnit 4的@Before,表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前执行

@AfterEach

类似于JUnit 4的@After,表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行。

@AfterAll

类似于JUnit 4的@AfterClass,表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行,必须为static

@DisplayName

为测试类或测试方法声明一个自定义的显示名称。

Spring Boot整合Junit 5_第1张图片

@TestPropertySource

单元测试中,经常需要对不同的用例设置不同的参数,可以使用这个注解帮助我们为每个单元测试类配置不同的变量

@TestPropertySource(properties = {
		"spring.data.mongodb.uri: mongodb://localhost:27017/Teach1"
})
@TestPropertySource(properties = {
		"spring.data.mongodb.uri: mongodb://localhost:27017/Teach2"
})

@Autowired

自动装载Bean

@Qualifier

与**@Autowired配合一起使用,可以指定使用具体哪个Bean**

你可能感兴趣的:(#,Junit,spring,boot,junit)