【Junit】Controller单元测试

本地开发环境说明

开发依赖版本
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进行测试

单元测试运行结果

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Idea Controller 是一个控制器类,负责处理与Idea模块相关的请求和逻辑。单元测试是一种测试方法,用于验证代码的功能是否正常。 对于 Idea Controller单元测试,我们需要验证以下几个方面的功能: 1. 路由功能:单元测试应该验证 Idea Controller 是否能够正确地接收和处理来自用户或其他模块的请求,并将请求转发到相应的方法。 2. 输入验证:应该验证 Idea Controller 是否能够正确地验证用户输入的数据,包括数据类型、长度、格式等,并对不合法的输入进行正确的处理,如返回错误信息或进行修正。 3. 业务逻辑:应该验证 Idea Controller 是否能够正确地根据业务规则进行数据处理,如对数据库的读写操作、数据的处理和计算等,并确保逻辑的正确性和完整性。 4. 错误处理:应该验证 Idea Controller 是否能够正确地处理异常情况,如数据库连接失败、请求超时、权限不足等,并能够返回正确的错误信息。 在进行 Idea Controller单元测试时,可以使用单元测试框架,如JUnit或TestNG,并使用模拟对象或桩对象来模拟其他依赖的模块或服务,以便进行独立的测试。 通过进行单元测试,我们可以提前发现和修复代码中的错误和问题,确保功能的正确性和稳定性,提高代码的质量和可维护性。此外,单元测试也可以帮助开发人员更好地理解和使用 Idea Controller 类的接口和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

太空眼睛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值