验证该设置器被嘲笑在模拟对象上被调用

发布于 2025-02-05 19:20:22 字数 504 浏览 2 评论 0原文

给定春季启动应用程序中的以下服务方法:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疑心病 2025-02-12 19:20:22

您正在寻找mockito.verify,并且一个测试看起来像:

@ExtendWith(MockitoExtension.class)
public class CategoryServiceTest {
    @Mock
    CategoryRepository categoryRepository;

    @InjectMocks
    CategoryService categoryService;

    @Test
    public void testUpdateCategoryMarksEntityDirty() {
        // given
        long categoryId = 1L;
        Category category = mock(Category.class);
        String newCategoryName = "NewCategoryName";
        when(categoryRepository.findById(categoryId)).thenReturn(Optional.of(category));

        // when
        categoryService.updateCategory(categoryId, new CategoryData(newCategoryName));

        // then
        verify(category, times(1)).setName(newCategoryName);
    }
}

但是,我必须建议不要使用这种测试方式。

您的代码表明您正在使用带有肮脏检查机制(JPA / Hibernate?)的DB访问库。您的测试重点是与您的数据库访问层的互动详细信息,而不是业务需求 - 更新成功保存在数据库中。

因此,我将选择针对真实数据库的测试,并采用以下步骤:

  • :then:and then the the the the the类别:
  • 给定: nocyserservice.update被调用
  • 以下:后续调用to categoryRepository.findbyid return return更新实体。

You are looking for Mockito.verify, and a test looking like:

@ExtendWith(MockitoExtension.class)
public class CategoryServiceTest {
    @Mock
    CategoryRepository categoryRepository;

    @InjectMocks
    CategoryService categoryService;

    @Test
    public void testUpdateCategoryMarksEntityDirty() {
        // given
        long categoryId = 1L;
        Category category = mock(Category.class);
        String newCategoryName = "NewCategoryName";
        when(categoryRepository.findById(categoryId)).thenReturn(Optional.of(category));

        // when
        categoryService.updateCategory(categoryId, new CategoryData(newCategoryName));

        // then
        verify(category, times(1)).setName(newCategoryName);
    }
}

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:

  • given: insert a Category into your DB
  • when: CategoryService.update is called
  • then: subsequent calls to categoryRepository.findById return updated entity.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文