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

}
相关推荐
叫我阿柒啊31 分钟前
从Java全栈到前端框架:一场真实面试的深度技术探索
java·redis·微服务·typescript·vue3·springboot·jwt
珠峰下的沙砾1 小时前
keycloak中对接oidc协议时设置prompt=login
java·服务器
IT_陈寒1 小时前
7个Vue 3.4新特性实战心得:从Composition到性能优化全解析
前端·人工智能·后端
BillKu1 小时前
Spring Boot 后端接收多个文件的方法
spring boot·后端·python
纤瘦的鲸鱼1 小时前
JUC 并发集合:高效处理多线程数据共享的利器
java·开发语言
hui函数1 小时前
订单后台管理系统-day07菜品模块
数据库·后端·python·flask
wr1 小时前
解决 NetMQ 创建Demo调试失败问题
后端
小菜全2 小时前
基于若依框架开发WebSocket接口
java·javascript·maven·mybatis·html5
瓯雅爱分享2 小时前
Java搭建高效后端,Vue打造友好前端,联合构建电子采购管理系统,实现采购流程电子化、自动化,涵盖采购全周期管理,功能完备,附详细可运行源码
java·mysql·vue·软件工程·源代码管理