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("所有测试已经结束。。。");
    }

}
相关推荐
孟健36 分钟前
出海开发者资金合规:从港卡结汇到完税申报实操
后端·架构
乌暮1 小时前
深入理解 Java 泛型:把「万能盒子」用对、用稳
java·开发语言·后端·学习
大猫和小黄1 小时前
深入理解 Java Optional:从设计哲学到工程实战
java
数据库百宝箱1 小时前
rum&gin索引对比
java·数据库·gin
架构技术专栏1 小时前
Skill 有效性与稳定性评估:用对照、重复与证据判断价值
后端
yychen_java2 小时前
第六篇:Spring AI 实战:将 Java 业务接口封装成企业级 MCP Server
java·人工智能·spring
拔剑纵狂歌2 小时前
ClickHouse system.query_log 字段详解与排障实践
后端·clickhouse·1024程序员节
_upupup2 小时前
异常(C++)
java·开发语言·jvm
g10565591392 小时前
公有云_云运维服务
java·运维·服务器
再吃一根胡萝卜2 小时前
把本地 RAG 问答做"准":多轮指代消解 + 云端语义向量 + TXT 章节切分
后端