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

相关推荐
-拟墨画扇-14 小时前
Git | Bug分支操作
git·gitee·github·bug·gitcode
小凡子空白在线学习14 小时前
Bug目录
bug
jiedaodezhuti2 天前
秒级定位线上Bug的一些命令
bug
l1t4 天前
修改一个触发PostgreSQL 17.2 bug的SQL
sql·postgresql·bug
包小黑4 天前
【Linux】bug登记好习惯:发现bug,用命令行截取对应日志
linux·bug
癫狂的兔子5 天前
【BUG】【Python】逆序取值为空
bug
癫狂的兔子5 天前
【BUG】【Python】精确度问题
python·bug
癫狂的兔子5 天前
【BUG】【Python】合并两个列表
bug
癫狂的兔子6 天前
【BUG】【Python】eval()报错
python·bug
余生H6 天前
Ai编程翻车修车记3 -一次因为移除监听器失败导致bug后的DOM事件深入学习
学习·bug·ai编程