改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分钟。

相关推荐
IT二叔13 小时前
Git Flow04-bug修改流程
git·bug
万粉变现经纪人1 天前
如何解决 pip install shapely 报错 GEOS C 库未找到 问题
c语言·开发语言·python·pycharm·bug·pandas·pip
cyforkk1 天前
前后端联调实战:解决业务异常被误判为成功的“幽灵 Bug”
bug·状态模式
li9056632802 天前
hanzi-writer-miniprogram Path2D问题以及Bug修复
微信小程序·bug
万粉变现经纪人2 天前
如何解决 pip install cx_Oracle 报错 未找到 Oracle Instant Client 问题
数据库·python·mysql·oracle·pycharm·bug·pip
ChoSeitaku3 天前
Git分支|创建分支|切换分支|合并分支|删除分支|合并冲突分支|分支策略|bug分支|强制删除分支
bug
Lxinccode4 天前
BUG(23) : node版claude code启动报错Failed to connect to api.anthropic.com: ETIMEDOUT
bug·claude·claude启动报错
buyulian5 天前
Bug防御体系:技术方案的优与劣
java·经验分享·bug·软件工程
川石课堂软件测试5 天前
接口测试需要注意的一些BUG
网络·数据库·python·单元测试·bug·压力测试·tornado
深念Y6 天前
记一个BUG:Trae里MongoDB和MySQL MCP不能共存
数据库·mysql·mongodb·ai·bug·agent·mcp