https://www.baeldung.com/mockito-spy
Wrap an instance of a real business object in a mockito spy, so that you can verify certain behavior was called
SubscriptionEntity entity = Mockito.spy(subscriptionEntity());
No, on the spy you can assert that a certain method was called
verify(entity, times(1)).setFcaSalesForceToken(FCA_SFO_TOKEN);
verify(repository, times(1)).saveSubscription(entity);
You can go even further and you can override existing behavior on a real business object, to customize it according to your needs during the testing: see here: https://www.baeldung.com/mockito-spy#stubbing-a-spy
@Test
public void whenStubASpy_thenStubbed() {
List<String> list = new ArrayList<String>();
List<String> spyList = Mockito.spy(list);
assertEquals(0, spyList.size());
Mockito.doReturn(100).when(spyList).size();
assertEquals(100, spyList.size());
}
In Spring you can Spy a Bean/Service like
@Autowired
@SpyBean
PriceService priceService;