间谍对象即使在stubbed时也会呼叫真实方法

发布于 2025-01-29 03:32:36 字数 1848 浏览 0 评论 0原文

我正在研究单元测试此服务方法,该方法需要使用正在测试的同一类中的其他方法。因此,我已经阅读了间谍,并将其应用于我的代码。但是,即使在我正在测试的类的间谍对象时,即使是我正在测试的类的间谍对象也会执行真实方法。

的方法

@Override
    public CategoryResponseDto updateCategory(Long categoryId, UpdateCategoryRequestDto requestDto) {
        CategoryEntity categoryEntity = findById(categoryId);

        modelMapper.convertToEntity(requestDto, categoryEntity);
        categoryEntity = categoryRepository.save(categoryEntity);

        return modelMapper.convertToResponse(categoryEntity, CategoryResponseDto.class);
    }

正在测试我的代码进行测试

    @Test
    public void updateCategory_ShouldReturnCategoryResponseDto_WhenDataValid() {
        CategoryEntity categoryEntity = mock(CategoryEntity.class);
        UpdateCategoryRequestDto requestDto = mock(UpdateCategoryRequestDto.class);
        CategoryCrudServiceImpl spy = Mockito.spy(categoryCrudService);
        CategoryResponseDto expectedResult = mock(CategoryResponseDto.class);

        doReturn(categoryEntity).when(spy).findById(anyLong());
        when(categoryRepository.save(categoryEntity)).thenReturn(categoryEntity);
        when(modelMapper.convertToResponse(categoryEntity, CategoryResponseDto.class)).thenReturn(expectedResult);

        CategoryResponseDto result = categoryCrudService.updateCategory(1L, requestDto);

        assertThat(result, is(expectedResult));
    }

,以便此处测试的类是categoryCrudserviceimple,该类的实例是categorycrudservice在我的测试代码中,我创建了间谍从中间谍(类别Crudservice)。如您所见,我将FindbyId()固定,该来自相同的categoryCrudserviceimple class class doretturn(categoreNtity)。 ;但它不起作用,我尝试传递任何()anylong()和真实的 findbyid()方法仍然被调用并抛出Resourcenotfound的例外。

你能帮我吗?我在这里失踪还是误解了什么?

非常感谢您的宝贵时间。

I am working on unit testing this service method, which requires using other methods in the same class that is being tested. So I have read about spy and applied it to my code. However, even when stubbed, the spy object of the class that I am testing keeps executing the real methods.

The method that is being tested

@Override
    public CategoryResponseDto updateCategory(Long categoryId, UpdateCategoryRequestDto requestDto) {
        CategoryEntity categoryEntity = findById(categoryId);

        modelMapper.convertToEntity(requestDto, categoryEntity);
        categoryEntity = categoryRepository.save(categoryEntity);

        return modelMapper.convertToResponse(categoryEntity, CategoryResponseDto.class);
    }

My code to test it

    @Test
    public void updateCategory_ShouldReturnCategoryResponseDto_WhenDataValid() {
        CategoryEntity categoryEntity = mock(CategoryEntity.class);
        UpdateCategoryRequestDto requestDto = mock(UpdateCategoryRequestDto.class);
        CategoryCrudServiceImpl spy = Mockito.spy(categoryCrudService);
        CategoryResponseDto expectedResult = mock(CategoryResponseDto.class);

        doReturn(categoryEntity).when(spy).findById(anyLong());
        when(categoryRepository.save(categoryEntity)).thenReturn(categoryEntity);
        when(modelMapper.convertToResponse(categoryEntity, CategoryResponseDto.class)).thenReturn(expectedResult);

        CategoryResponseDto result = categoryCrudService.updateCategory(1L, requestDto);

        assertThat(result, is(expectedResult));
    }

So the class being tested here is CategoryCrudServiceImple, the instance of that class is categoryCrudService in my test code and I create a spy out of it spy(categoryCrudService). As you can see, I stubbed the findById() which comes from the same CategoryCrudServiceImple class with doReturn(categoryEntity).when(spy).findById(1L); and It didn't work, I tried passing in any() or anyLong() and the real findById() method is still called and throws the exception of ResourceNotFound.

Could you please help me with this? Am I missing or misunderstanding anything here?

Thank you very much for your precious time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

↘紸啶 2025-02-05 03:32:36

好吧,多亏了上述@TOM的评论,我设法做到了,因为我没有在此处使用spy categoryResponsedto result = categoryCrudservice.updateCategory(1L,requestDto);。应该像这样的categoryResponsed to result = spy .updatecategory(1L,requestDTO);

Well, thanks to the above comment of @Tom, I managed to get it right because I did not use spy here CategoryResponseDto result = categoryCrudService.updateCategory(1L, requestDto);. It should be like this CategoryResponseDto result = spy .updateCategory(1L, requestDto);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文