改BUG:Mock测试的时候,when失效

问题再现:

这里我写了一测试用户注册接口的测试类,并通过when模拟下层的服务,但实际上when并没有奏效,还是走了真实的service层的逻辑。

java 复制代码
package cn.ac.evo.review.test;

import cn.ac.evo.review.user.UserMainApplication;
import cn.ac.evo.review.user.register.controller.UserRegisterController;
import cn.ac.evo.review.user.register.model.dto.RegisterDTO;
import cn.ac.evo.review.user.register.service.IUserRegisterService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

/**
 * @author urfread
 * @date 2025-02-20 09:34
 */
@SpringBootTest(classes = UserMainApplication.class)
@AutoConfigureMockMvc
public class UserRegisterTest {
    @Autowired
    private MockMvc mockMvc;
    private RegisterDTO registerDTO;

    @Mock
    private IUserRegisterService userRegisterService;  // 模拟服务层

    @BeforeEach
    public void setUp() {
        registerDTO = new RegisterDTO("3131015733@qq.com", "123456", "123456");
    }

    // 测试邮箱是否已注册
    @Test
    public void testCheckEmailRegisteredSuccess() throws Exception {
        // 模拟服务层返回的结果
        when(userRegisterService.checkEmailRegistered(registerDTO.getEmail())).thenReturn(true);

        mockMvc.perform(get("/api/user/register/checkEmailRegistered")
                        .param("email", registerDTO.getEmail()))
                .andExpect(status().isOk())
                .andExpect(content().string("true"));
    }

    @Test
    public void testCheckEmailRegisteredFail() throws Exception {
        // 模拟服务层返回的结果
        when(userRegisterService.checkEmailRegistered(registerDTO.getEmail())).thenReturn(false);

        mockMvc.perform(get("/api/user/register/checkEmailRegistered")
                        .param("email", registerDTO.getEmail()))
                .andExpect(status().isOk())
                .andExpect(content().string("false"));
    }
}

解决

只改一行就可以

java 复制代码
@Mock
private IUserRegisterService userRegisterService;  // 模拟服务层

把这里的 @Mock 改为 @MockBean,然后 when 就又奏效了。

就是这么简单,有研究了20分钟。

相关推荐
东巴图13 小时前
分解如何利用c++修复小程序的BUG
开发语言·c++·bug
workflower3 天前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程
lvchaoq4 天前
记录小程序真机bug,而模拟器无法复现
小程序·bug
喜欢便码4 天前
禅道提交bug的几种状态
bug
从前慢,现在也慢4 天前
(3)Bug篇
学习·bug·测试
西柚小萌新4 天前
【Bug:docker】--Docker国内镜像源加载失败
docker·容器·bug
初圣魔门首席弟子6 天前
c++ bug 记录(merge函数调用时错误地传入了vector对象而非迭代器。)
java·c++·bug
Qiuner7 天前
历劫波,明真我——Debug Commune
ai·开源·bug·debug·信息差·信息·交流
川石课堂软件测试8 天前
什么是BUG,你对BUG的了解有多少?
android·linux·服务器·python·功能测试·bug·安全性测试