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

}
相关推荐
allenXer3 小时前
Spring Boot测试全景指南:JUnit 5 + Testcontainers实现单元与集成测试
spring boot·微服务·log4j
cui_hao_nan3 小时前
JVM——如何对java的垃圾回收机制调优?
java·jvm
心平愈三千疾5 小时前
学习秒杀系统-实现秒杀功能(商品列表,商品详情,基本秒杀功能实现,订单详情)
java·分布式·学习
玩代码5 小时前
备忘录设计模式
java·开发语言·设计模式·备忘录设计模式
BUTCHER56 小时前
Docker镜像使用
java·docker·容器
岁忧6 小时前
(nice!!!)(LeetCode 面试经典 150 题 ) 30. 串联所有单词的子串 (哈希表+字符串+滑动窗口)
java·c++·leetcode·面试·go·散列表
晴空月明6 小时前
分布式系统高可用性设计 - 监控与日志系统
后端
songroom7 小时前
【转】Rust: PhantomData,#may_dangle和Drop Check 真真假假
开发语言·后端·rust
LJianK17 小时前
Java和JavaScript的&&和||
java·javascript·python