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

}
相关推荐
sql2008help26 分钟前
使用spring-boot-starter-validation实现入参校验
java·开发语言
Mr_Air_Boy31 分钟前
springboot集成xxl-job
java·spring boot·spring
Babybreath-1 小时前
Tomcat
java·tomcat
摇滚侠1 小时前
面试实战 问题二十三 如何判断索引是否生效,什么样的sql会导致索引失效
java
悟纤2 小时前
当生产环境卡成 PPT:Spring Boot 线程 Dump 捉妖指南 - 第544篇
java·spring boot·后端
江影影影3 小时前
Spring Boot 2.6.0+ 循环依赖问题及解决方案
java·spring boot·后端
快乐就是哈哈哈3 小时前
《JSR303 数据校验全攻略:从入门到实战,玩转优雅的参数验证》
后端
快乐就是哈哈哈4 小时前
Linux 部署与管理 Spring Boot 项目保姆级全流程
后端
codeGoogle4 小时前
昇腾揭开的伤疤,刺痛了谁?
后端
null不是我干的4 小时前
黑马SpringBoot+Elasticsearch作业2实战:商品搜索与竞价排名功能实现
spring boot·后端·elasticsearch