改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("[email protected]", "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分钟。

相关推荐
岳轩子3 小时前
git更新的bug
git·bug
DADONGOOO3 小时前
Ultralytics中的RT-DETR模块的RepC3 bug
bug
waves浪游8 小时前
自动化测试常用函数
测试用例·bug·测试
介si啥呀~10 小时前
解决splice改变原数组的BUG(拷贝数据)
java·前端·bug
sheepfagdng10 小时前
软件测试——BUG概念
bug·压力测试
Z_z在努力16 小时前
【软件测试】bug 篇
bug·测试
upp17 小时前
[bug]langchain agent报错Invalid Format: Missing ‘Action Input:‘ after ‘Action:‘
javascript·python·langchain·bug
hunter12718 小时前
Dolphinscheduler3.2.1运行Java Jar路径重复的BUG修复问题
java·bug·jar
achonor19 小时前
Unity UGUI Image使用图集透明度点击过滤BUG
unity·游戏引擎·bug
Test-Sunny1 天前
多个定时器同时工作时,会出现哪些常见的bug ,如何解决??(定时任务未实时更新但刷新后正常的问题分析)
功能测试·bug·集成测试