NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • 设计模式

  • 开发规范

  • 经验分享

  • 记录

  • 快速开始

    • Spring Boot整合RabbitMQ
    • Spring Boot整合Email邮件发送
    • Spring Boot整合jasypt加密配置文件
    • Spring Boot整合单元测试
      • pom.xml
      • BaseTest.java
      • 参考
    • Spring Boot整合优雅关机
    • Spring Boot整合Redis分布式锁
    • Spring Boot整合MyBatis-plus
    • XXL-JOB快速上手
    • Spring Boot整合WebSocket(stomp协议)
    • SpringBoot整合i18n(多语言)
    • 第三方登录 - Google
    • 第三方登录 - Facebook
    • Spring Boot 整合Elasticsearch
  • 笔记

  • 面试题

  • 微服务

  • 踩过的坑

  • Java
  • 快速开始
NipGeihou
2022-01-16
目录

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 注解

# 参考

  • SpringBoot Test 及注解详解 - codedot - 博客园 (opens new window)
  • Getting Started | Testing the Web Layer (opens new window)
上次更新: 2022/12/31, 03:04:26
Spring Boot整合jasypt加密配置文件
Spring Boot整合优雅关机

← Spring Boot整合jasypt加密配置文件 Spring Boot整合优雅关机→

最近更新
01
元器件
05-23
02
iSCSI服务搭建
05-10
03
磁盘管理与文件系统
05-02
更多文章>
Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式