springboot项目或其他项目使用@Test测试项目接口配置-spring-boot-starter-test

快速入门

添加依赖

xml 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

创建测试目录

正式使用和启动类一样的目录

使用

java 复制代码
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.danny.RuoYiDannyApplication;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


// 如果报错可以使用下面的直接导入:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
//@SpringBootTest
@SpringBootTest(classes = RuoYiDannyApplication.class)
@DisplayName("junit5功能测试,在idea下放显示自定义名称")
public class MyServiceTest {
    // 测试代码
    @Autowired
    private RedisService redisService;


    @Test
    public void test1() {
        System.out.println("测试1");
        redisService.setCacheObject("hello", "world");
        String hello = redisService.getCacheObject("hello");
        System.out.println(hello);
    }

    @DisplayName("在idea下放显示自定义名称")
    @Test
    void testDisplayName() {
        System.out.println("测试在idea下放显示自定义名称");
    }

    /**
     * 测试前置条件
     */
    @DisplayName("测试前置条件")
    @Test
    void testAssumptions() {
        Assumptions.assumeTrue(false, "结果不足true");
        System.out.println("11111");
    }

    @BeforeEach
    void testBeforeEach() {
        System.out.println("测试就要开始。。。");
    }

    @AfterEach
    void testAfterEach() {
        System.out.println("测试就要结束。。。");
    }

    @BeforeAll
    static void testBeforeAll() {
        System.out.println("所有测试就要开始。。。");
    }

    @AfterAll
    static void testAfterAll() {
        System.out.println("所有测试已经结束。。。");
    }

}
相关推荐
马优晨3 小时前
Spring Boot + MyBatis + Redis 整合实战 —— 项目源码深度解析
spring boot·redis·mybatis·mybatis + redis·redis实战·spring实战·mybatis实战
IKUN家族8 小时前
Spring MVC(一)
java·spring·mvc
老马识途2.09 小时前
关于跨域问题的总结
java·前端
都叫我大帅哥10 小时前
Java日期时间三十年战争:从Date考古到LocalDateTime革命,以及数据库与前端的那点事儿
java
Muscleheng10 小时前
SpringBoot 集成 DeepSeek 实现 RAG 文档问答
java·spring boot·ai·springai
2501_9364156911 小时前
可变参数&综合练习&斗地主游戏
java·windows·游戏
颜酱11 小时前
07 | 把字段与指标同步到 Qdrant(生成阶段)
前端·人工智能·后端
Larcher12 小时前
从“加载模型”界面到端侧推理:拆解一个 React + WebGPU 大模型 Demo
javascript·后端
极客先躯12 小时前
高级java每日一道面试题-2026年05月03日-实战篇[Docker]-如何实现容器化环境的数据加密?
java·运维·docker·容器·金融·加解密·高级面试
xqqxqxxq12 小时前
Java Socket 多人聊天室(私聊+群聊)技术笔记(V4版本)
java·网络·笔记