 Spring Boot整合单元测试
Spring Boot整合单元测试
  # pom.xml
本文编写时使用的版本为 2.5.6
<!-- SpringBoot 单元测试-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
# BaseTest.java
package com.xxx;
import com.xxx.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
 * @author NipGeihou
 * @create 2021-10-30 10:03
 */
@Slf4j
// 暂不清楚这个注解是不是Junit4的,在当前版本已经改用Junit5,无需也不能使用此注解
// @RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {
    @Autowired
    private RedisCache redisCache;
    @Test
    public void test() {
        redisCache.setCacheObject("name", "NipGeihou");
        log.info(redisCache.getCacheObject("name"));
    }
}
需要 Spring 容器(依赖注入)时在测试类添加
@SpringBootTest注解
# 参考
上次更新: 2022/12/31, 03:04:26
