Spring Boot 1.5.x 结合 JUnit5 进行接口测试

spring,boot,结合,junit5,进行,接口,测试 · 浏览次数 : 34

小编点评

## Spring Boot 1.5.x + Junit4 测试类代码示例 以下是使用 Spring Boot 1.5.x 和 Junit4 测试控制器接口的代码示例,并在升级至 Junit5 时依然可以正常运行的版本: ```java @SpringBootTest @RunWith(SpringRunner.class) public class DemoControllerTest { @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void setup() { // 使用 Junit5 的 SpringExtension 来配置 WebApplicationContext MockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(webApplicationContext); mockMvc = builder.build(); } @Test public void demoTest() { MvcResult mvcResult = mockMvc.perform(post("/api/demo") .contentType(MediaType.APPLICATION_JSON) .content(jsonStr)) .andExpect(jsonPath("$.data").values(expectedValue)) .andDo(print()) .andReturn(); } } ``` **说明:** * 代码首先使用 `@SpringBootTest` 注解配置 Spring Boot 应用上下文,并使用 `@Autowired` 注解注入 `WebApplicationContext` 对象。 * `@MockMvc` 注解用于创建 MockMvc 对象,并使用 `webAppContextSetup()` 方法构建 `MockMvc` 对象。 * `@Before` 方法在测试开始之前运行,使用 `MockMvcBuilders` 创建 `MockMvc` 对象,并使用 `webAppContextSetup()` 方法为 `MockMvc` 设置 ApplicationContext。 * `@Test` 方法测试 `demoTest` 方法,并使用 `MvcResult` 和 `jsonPath()` 等方法来验证测试结果。 **升级 Junit5 的版本:** 如果你已经升级了 Junit5 版本,由于 Junit5 不支持 `@RunWith` 注解,无法获取到 `WebApplicationContext` 对象,测试将无法运行。为了解决这个问题,您可以使用 `@ExtendWith(SpringExtension.class)` 等方法来配置 Spring Boot 应用上下文,并使用 `MockMvc` 等工具来模拟控制器。

正文

在Spring Boot 1.5.x中,默认使用Junit4进行测试。而在对Controller进行接口测试的时候,使用 @AutoConfigureMockMvc 注解是不能注入 MockMvc 对象的。因此只能使用 WebApplicationContext 类去构建 MockMvc 对象。
在Spring Boot 1.5.x + Junit4 的前提下,测试类的代码是这样写的:

@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoControllerTest {
    @Autowired
    private WebApplicationContext webApplicationContext;
    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void demoTest() {
        MvcResult mvcResult = mockMvc.perform(post("/api/demo")
                              .contentType(MediaType.APPLICATION_JSON)
                              .content(jsonStr))
                              .andExpect(jsonPath("$.data").values(expectedValue))
                              .andDo(print())
                              .andReturn();
    }
}

但是,当我们把Junit版本升级到Junit5时,由于Junit5不再支持@RunWith注解,导致我们无法获取到 WebApplicationContext 对象,测试也就无法运行了。
经过网上各种搜索,终于找到一个既能完成测试,又不需要升级 Spring Boot 版本的方法。
我们在pom.xml中引入如下包

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
<!-- ... -->
    <dependency>
        <groupId>com.github.sbrannen</groupId>
        <artifactId>spring-test-junit5</artifactId>
        <version>1.5.0</version>
        <scope>test</scope>
    </dependency>

这样就可以在测试类上加上 @ExtendWith(SpringExtension.class),使 WebApplicationContext 的对象可以被自动注入了。

spring-test-junit5 的 github 地址见 https://github.com/sbrannen/spring-test-junit5

与Spring Boot 1.5.x 结合 JUnit5 进行接口测试相似的内容:

Spring Boot 1.5.x 结合 JUnit5 进行接口测试

在Spring Boot 1.5.x中,默认使用Junit4进行测试。而在对Controller进行接口测试的时候,使用 @AutoConfigureMockMvc 注解是不能注入 MockMvc 对象的。因此只能使用 WebApplicationContext 类去构建 MockMvc 对象。 在

Springboot+Mybatisplus+ClickHouse集成

# 核心依赖引入 ```xml ru.yandex.clickhouse clickhouse-jdbc 0.1.53 com.baomidou mybatis-plus-boot-starter 3.5.1 org.springframework.boot spring-boot-starter-

[转帖]总结:Springboot监控Actuator相关

一、介绍 由于项目中使用的仍然是比较老旧的1.5.6版本,所以本文是基于此版本进行描述。 二、Actuator使用 ActuatorActuator是Spring Boot提供的对应用系统的监控和管理的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的Spring beans信息、系统

allowedOrigins cannot contain the special value "*"

Spring Boot的版本高于 2.4以后 ,原来的配置已经不适合目前的版本 将代码中的`allowedOrigins`改为`allowedOriginPatterns` ```java @Configuration public class WebConfig implements WebMvc

springboot升级过程中踩坑定位分析记录 | 京东云技术团队

因所负责的系统使用的spring框架版本5.1.5.RELEASE在线上出过一个偶发的小事故,最后定位为spring-context中的一个bug导致的。

Spring Boot中发送邮件时,如何让发件人显示别名

之前,我们通过一系列文章,介绍了如何在Spring Boot中发送邮件: 发送邮件 添加附件 引用静态资源 邮件模版 已经包含了大部分的应用场景。但最近DD在做YouTube中文配音的时候,碰到一个问题: 如上图所示,收件人在客户端收到的时候,显示的名称是邮箱的前缀,而不是我们的产品名称,也就是邮箱

[转帖]Skywalking学习及整合springboot

目录 1. Skywalking概述 2. Skywalking主要功能 3. Skywalking主要特性 4. Skywalking架构简介 5. Spring Cloud与Skywalking实战 5.1 Skywalking部署构建 5.3 Spring Cloud整合Skywalking实

[转帖]一次SpringBoot版本升级,引发的血案

https://z.itpub.net/article/detail/B6495288E725529E58105397659A08EB 前言 近项目组升级了SpringBoot版本,由之前的2.0.4升级到新版本2.7.5,却引出了一个大Bug。 到底是怎么回事呢? 1.案发现场 有一天,项目组的同

k8s部署springboot

# 综述 首先介绍发布步骤。 1.从代码仓库下载代码,比如GitLab; 2.接着是进行打包,比如使用Maven; 3.编写Dockerfile文件,把步骤2产生的包制作成镜像; 4.上传步骤3的镜像到远程仓库,比如Harhor; 5.编写Deployment文件; 6.提交Deployment文件

K8S发布应用步骤详解

前言 首先以SpringBoot应用为例介绍一下k8s的发布步骤。 1.从代码仓库下载代码,比如GitLab; 2.接着是进行打包,比如使用Maven; 3.编写Dockerfile文件,把步骤2产生的包制作成镜像; 4.上传步骤3的镜像到远程仓库,比如Harhor; 5.编写Deployment文