Spring Boot整合Junit 5

Spring Boot整合Junit 5

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

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

环境介绍

软件名称软件版本
Spring Boot2.5.3
Maven3.6.3

搭建一个maven工程

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

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.5.3</version>
</parent>

编写一个单元测试的父类

/**
 * 单元测试可以继承此类。
 * 
 * @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

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

@DisplayName注解使用效果图

@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**

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太空眼睛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值