title: Spy object keeps calling to real method even when stubbed
body: <p>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 <code>spy</code> 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.</p>
  <p>The method that is being tested</p>
  <pre><code>@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);
      }
  </code></pre>
  <p>My code to test it</p>
  <pre><code>    @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));
      }
  </code></pre>
  <p>So the class being tested here is <code>CategoryCrudServiceImple</code>, the instance of that class is <code>categoryCrudService</code> in my test code and I create a spy out of it <code>spy(categoryCrudService)</code>. As you can see, I stubbed the <code>findById()</code> which comes from the same <code>CategoryCrudServiceImple</code> class with <code>doReturn(categoryEntity).when(spy).findById(1L);</code> and It didn't work, I tried passing in <code>any()</code> or <code>anyLong()</code> and the real <code>findById()</code> method is still called and throws the exception of <code>ResourceNotFound</code>.</p>
  <p>Could you please help me with this? Am I missing or misunderstanding anything here?</p>
  <p>Thank you very much for your precious time.</p>