【Junit】Controller单元测试

文章展示了在SpringBoot3.0.6和JDK20的配置下,如何创建一个简单的Controller并编写单元测试。Controller用于处理/demo请求,返回随机字母字符串。单元测试使用了MockMvc和WebTestClient对Controller的功能进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本地开发环境说明

开发依赖版本
Spring Boot3.0.6
JDK20

pom.xml主要依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

写一个Controller

package com.wen3.framework.junit.controller;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @RequestMapping(path = "/demo")
    public String demo() {
        return RandomStringUtils.randomAlphabetic(10);
    }
}

单元测试

package com.wen3.junit.demo;

import com.wen3.framework.junit.Application;
import com.wen3.framework.junit.controller.DemoController;
import jakarta.annotation.Resource;
import jakarta.servlet.Filter;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;

import java.util.List;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
@AutoConfigureWebTestClient
public class MockControllerTest extends Assertions {

    @Resource
    protected MockMvc mvc;
    protected static MockMvc mvc2;
    @Autowired(required = false)
    WebTestClient webClient;

    @BeforeAll
    public static void beforeAll(WebApplicationContext wac) {
        // 增加过滤器
        mvc2 = MockMvcBuilders.webAppContextSetup(wac).addFilters(new CharacterEncodingFilter("UTF-8")).build();
    }

    @SneakyThrows
    @Test
    void demo() {
        MvcResult mvcResult = mvc.perform(get("/demo")).andReturn();
        MockHttpServletResponse mockHttpServletResponse = mvcResult.getResponse();
        String body = mockHttpServletResponse.getContentAsString();
        assertFalse(body.isEmpty());

        mvcResult = mvc2.perform(get("/demo")).andReturn();
        mockHttpServletResponse = mvcResult.getResponse();
        body = mockHttpServletResponse.getContentAsString();
        assertFalse(body.isEmpty());
    }
}
  • 可以通过@AutoConfigureMockMvc注解,直接注入MockMvc
  • 也可以使用MockMvcBuilders手工build一个MockMvc
  • 也可以使用WebTestClientController进行测试

单元测试运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太空眼睛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值