验证该设置器被嘲笑在模拟对象上被调用
给定春季启动应用程序中的以下服务方法:
@Transactional
public void updateCategory(long categoryId, CategoryData categoryData) {
final Category category = categoryRepository.findById(categoryId).orElseThrow(EntityNotFoundException::new);
category.setName(categoryData.getName());
}
我知道如何指示莫科托模拟categoryrepository.findbyid()
结果。
但是,我还不能弄清楚:是否有可能验证category.setName()
以categorydata.getname()
的确切参数调用?
Given the following service method in a Spring Boot application:
@Transactional
public void updateCategory(long categoryId, CategoryData categoryData) {
final Category category = categoryRepository.findById(categoryId).orElseThrow(EntityNotFoundException::new);
category.setName(categoryData.getName());
}
I know how to instruct Mockito to mock the categoryRepository.findById()
result.
However, I couldn't figure out yet: Is it possible to verify that category.setName()
was called with the exact argument of categoryData.getName()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找
mockito.verify
,并且一个测试看起来像:但是,我必须建议不要使用这种测试方式。
您的代码表明您正在使用带有肮脏检查机制(JPA / Hibernate?)的DB访问库。您的测试重点是与您的数据库访问层的互动详细信息,而不是业务需求 - 更新成功保存在数据库中。
因此,我将选择针对真实数据库的测试,并采用以下步骤:
You are looking for
Mockito.verify
, and a test looking like:I must, however, advise against this style of testing.
Your code suggests that you are using a DB Access library with dirty-checking mechanism (JPA / Hibernate?). Your test focuses on the details of interaction with your DB Access layer, instead of business requirement - the update is successfully saved in the DB.
Thus, I would opt for a test against a real db, with following steps: