单元测试 Mockito

博客:fearless’s blog

Mock 静态方法

  • // 导包,注意如果导入junit.jupiter执行时会报错,需要将类、方法全部改为public,@BeforeEach改为@Before
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    // 类注解
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({SecurityUtils.class})
    @Transactional(transactionManager = "transactionManager")
    @SpringBootTest
    // mock
    PowerMockito.mockStatic(SecurityUtils.class);
    PowerMockito.when(SecurityUtils.getDeptId()).thenReturn("1");
    

Mock mybatisPlus baseMapper

  • // mock mapper
    @Mock
    private ShopMallEntranceMapper shopMallEntranceMapper;
    // mock baseMapper
    ReflectionTestUtils.setField(shopMallEntranceService, "baseMapper", shopMallEntranceMapper);
    // 使用对应mapper代替baseMapper调用即可
    Mockito.when(shopMallEntranceMapper.insert(Mockito.any())).thenReturn(1);
    

Mock mybatisPlus service this.saveBatch方法

  • // Mockito.spy()可以在真实对象上创建间谍(类似于浅克隆),尝试在创建的间谍对象上打桩,用于mock本类this调用
    CouponAndPoolRelationshipV2ServiceImpl service = Mockito.spy(couponAndPoolRelationshipV2Service);
    // 用spy service对象调用saveBatch
    Mockito.when(service.saveBatch(Mockito.any())).thenReturn(Boolean.TRUE);
    

Mock mybatisPlus service this.remove方法

  • // 不能用service或spy service直接mock,只能mock mapper
    Mockito.when(couponAndPoolRelationshipV2Mapper.delete(Mockito.any())).thenReturn(1);
    

Mock 特殊类

  • // Mock文件类
    MockMultipartFile file = new MockMultipartFile("测试", "测试".getBytes());
    // Mock HttpServletRequest
    MockHttpServletRequest request = new MockHttpServletRequest();
    

Mock MVC调用

  • // 注入WebApplicationContext
    @Autowired
    protected WebApplicationContext wac;
    // 初始化MockMvc对象
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  
    // Mock MVC
    String reqVo = "XK5H";
    mockMvc.perform(MockMvcRequestBuilders.get("/area/listByParentAuth")
                    .contentType(MediaType.APPLICATION_JSON)
                    .param("parentAuth", reqVo))
            .andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isOk());
    

Mock 错误记录

  • // 1.错误解决:Caused by: java.security.NoSuchAlgorithmException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext
    // 加类注解
    @PowerMockIgnore("javax.net.ssl.*")
    // 2.错误解决:com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not find lambda cache for this entity[com.domain.ThirdJinkeActivityMemberScore]
    // lambda cache 为null问题解决
    TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), ThirdJinkeActivityMemberScore.class);
    

公众号:
在这里插入图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fearIess233

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值