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

相关推荐
chao_78918 小时前
针对“仅某个地区出现Bug”的原因分析与解决方案
测试用例·bug
帅帅哥的兜兜18 小时前
Bug问题
bug
养意18 小时前
git提交代码和解决冲突修复bug
git·bug
东方不败之鸭梨的测试笔记1 天前
20250605车充安服务器受木马攻击导致服务不可用
bug
Htht1111 天前
【Qt】之【Get√】【Bug】通过值捕获(或 const 引用捕获)传进 lambda,会默认复制成 const
数据库·bug
前端发现2 天前
如何用 pnpm patch 给 element-plus 打补丁修复线上 bug(以 2.4.4 修复 PR#15197 为例)
bug
可乐鸡翅好好吃2 天前
通过BUG(prvIdleTask、pxTasksWaitingTerminatio不断跳转问题)了解空闲函数(prvIdleTask)和TCB
c语言·stm32·单片机·嵌入式硬件·bug·keil
神膘护体小月半2 天前
bug 记录 - 使用 el-dialog 的 before-close 的坑
前端·javascript·bug
顽强d石头3 天前
bug:undefined is not iterable (cannot read property Symbol(Symbol.iterator))
前端·bug
阿松のblog3 天前
opencv使用经典bug
人工智能·opencv·bug