【问题标题】:A Mocked repository returns null - Springboot JUnit Mockito模拟存储库返回 null - Springboot JUnit Mockito
【发布时间】:2021-05-08 19:19:28
【问题描述】:

我正在尝试使用我的存储库 EmpresaRepository 中的保存,但它返回 null,这是我的存储库 heres my repository and this is my test

And this is the output

【问题讨论】:

  • 长期 SO 用户的提示:内联代码和输出示例,而不是仅仅链接它们,会增加有人回答的几率。作为读者,这对我来说不那么麻烦,我可以一起查看所有相关信息。

标签: mockito junit5 spring-boot-test


【解决方案1】:

这是预期的行为。

通过使用@MockBean,您可以创建EmpresaRepository 的Mock 并将其添加到ApplicationContext。 这意味着您执行模拟的 ::save() 方法,该方法返回 null 默认值。

经验法则:永远不要模拟你想要测试的东西。

如果您将@MockBean 注释替换为Autowired,测试应该可以工作。


顺便说一句。 请不要发布您的代码的屏幕截图。在不同的选项卡之间切换很烦人,而且我无法复制您的部分代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多