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

}
相关推荐
升鲜宝供应链及收银系统源代码服务14 分钟前
升鲜宝供应链管理系统-生鲜配送系统_分拣端界面重构设计(一)
spring boot·重构·开源·收银系统·生鲜门店·升鲜宝多门店收银系统
JaguarJack19 分钟前
PHP 基金会宣布:Streams 现代化 将引入事件循环与异步新能力
后端·php
lzjava202421 分钟前
Spring AI实现一个智能客服
java·人工智能·spring
Mos_x27 分钟前
HeidiSQL导入与导出数据
java·后端
oak隔壁找我40 分钟前
Elasticsearch QueryBuilders 高级使用案例
java·后端
青云交42 分钟前
Java 大视界 -- Java 大数据在智能家居能源消耗模式分析与节能策略制定中的应用
java·大数据·智能家居·数据采集·能源消耗模式分析·节能策略制定·节能效果评估
Zhang青山44 分钟前
【玩转全栈】----Django基本配置和介绍
java·后端
BUG?不,是彩蛋!1 小时前
Java Web 项目打包部署全解析:从 IDEA 配置到 Tomcat 运行
java·intellij-idea
勇敢牛牛_2 小时前
Rust真的适合写业务后端吗?
开发语言·后端·rust
JIngJaneIL2 小时前
财务管理|基于SprinBoot+vue的个人财务管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·财务管理系统