当前位置: 首页 > article >正文

SpringBoot 单元测试 - 登录认证在 Spring Boot 上的标准单元测试写法。

👉 请投票支持这款 全新设计的脚手架 ,让 Java 再次伟大!

在这里插入图片描述

不要使用 @SpringBootTest

使用 @SpringBootTest 进行单元测试会启动整个 Spring Boot 容器,并引入整个项目的 development&test 依赖。缺点是速度慢、体积大、测试目标不明确、低内聚高耦合。

明确我们要测试的目标是登录功能,所以只要启动 Spring Mvc 的依赖范围就可以了,其他层面的依赖可以用「打桩」来解决。

使用 @WebMvcTest

所以只需要隔离启动 Spring Mvc 环境,即可完成登录功能的测试了。

@WebMvcTest(value = {SignController.class})
@Import({HttpFireWallConfig.class})
class SignMvcTest {

  @MockBean private SignService signService;

  @MockBean private CookieJwt cookieJwt;

  @Autowired private MockMvc mockMvc;

  @Test
  @WithMockUser
  void signIn_givenValidHttpRequest_shouldSucceedWith200() throws Exception {
    String stubUsername = "test_04cb017e1fe6";
    String stubPassword = "test_567472858b8c";
    SignInDto signInDto = new SignInDto();
    signInDto.setUsername(stubUsername);
    signInDto.setPassword(stubPassword);

    when(signService.signIn(signInDto)).thenReturn(1L);
    mockMvc
        .perform(
            post("/auth/sign-in")
                .contentType(MediaType.APPLICATION_JSON)
                .content(
                    """
                {
                  "username": "test_04cb017e1fe6",
                  "password": "test_567472858b8c"
                }
                 """)
                .with(csrf()))
        .andExpect(status().isOk());
  }

 @Test
  @WithMockUser
  void signIn_givenInValidHttpRequest_shouldFailedWith400() throws Exception {
    String stubUsername = "test_04cb017e1fe6";
    String stubPassword = "test_567472858b8c";
    SignInDto signInDto = new SignInDto();
    signInDto.setUsername(stubUsername);
    signInDto.setPassword(stubPassword);

    when(signService.signIn(signInDto)).thenReturn(1L);
    mockMvc
        .perform(
            post("/auth/sign-in")
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .content(
                    """
                   {
                     "username": "test_04cb017e1fe6",
                     "password": "test_567472858b8c"
                   }
                    """)
                .with(csrf()))
        .andExpect(status().isBadRequest());

    when(signService.signIn(signInDto)).thenReturn(1L);
    mockMvc
        .perform(
            post("/auth/sign-in")
                .contentType(MediaType.APPLICATION_JSON)
                .content(
                    """
                       {
                         "username": "test_04cb017e1fe6"
                       }
                        """)
                .with(csrf()))
        .andExpect(status().isBadRequest());
  }
}

更多单元测试的写法

更多的区分了隔离环境的单元测试代码,都集成到了下面的项目中:👇
一款全新设计的脚手架 ,让 Java 再次伟大!
在这里插入图片描述


http://www.kler.cn/news/361134.html

相关文章:

  • DruidDataSource 封clickhouse实现数据操作
  • 序列化问题记录:Jackson 与 Fastjson 的注解
  • 【YOLO学习】YOLOv5详解
  • Turn-it:优化线材重构雕塑制造
  • Java全栈经典面试题剖析6】JavaSE高级 -- 文件、IO流、序列化
  • 【计算机网络】详解数据链路层数据帧Mac地址ARP协议
  • Jetpack架构组件_LiveData组件
  • 【贪心算法】(第八篇)
  • kali——strings的使用
  • 安利一款基于canvas/svg的富文本编辑器-支持在导出PDF、DOCX
  • 华为三层交换来实现不同vlan通信问题
  • Redis-04 Redis管道
  • Flink任务报错akka size oversized
  • 基于 Hugo 的静态响应式网址导航主题
  • sh与bash的区别
  • Linux 防火墙的开启、关闭、禁用命令
  • SpringMVC 中的常用注解和用法
  • 探索Web3生态系统:社区、协议与参与者的角色
  • 详解ip route
  • SpringBoot民宿预订系统:打造在线住宿新体验