【Junit】Controller单元测试

本地开发环境说明

开发依赖 版本
Spring Boot 3.0.6
JDK 20

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进行测试

单元测试运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/friendlytkyj/article/details/131022675