间谍对象即使在stubbed时也会呼叫真实方法
我正在研究单元测试此服务方法,该方法需要使用正在测试的同一类中的其他方法。因此,我已经阅读了间谍
,并将其应用于我的代码。但是,即使在我正在测试的类的间谍对象时,即使是我正在测试的类的间谍对象也会执行真实方法。
的方法
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,多亏了上述@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 thisCategoryResponseDto result = spy .updateCategory(1L, requestDto);
.