I am tyring to partially mock a spring bean so that only one of the methods called within another method is mocked by the Spy
@ActiveProfiles(profiles = {Profiles.TEST})
public class ServiceTestMockito {
@Autowired
private ServiceBean serviceBean;
@Autowired
private DAO dao;
@Test
public void testSpy() {
ServiceBean serviceBeanSpy = Mockito.spy(serviceBean);
doReturn(true).when(serviceBeanSpy).methodB(Mockito.any());
Assert.isTrue(serviceBeanSpy.methodA(new Employee()), "This should be true");
}
}
public class ServiceBean {
public Boolean methodB(Employee employee) {
return false;
}
public Boolean methodA(Employee employee){
return methodB(employee);
}
}
I used a debugger to see if serviceBean is an different instance than serverBeanSpy but turns out they are the same. I am not sure what am I missing here
I did refer to the following questions but everyone seems to find the issue with the way mock is declared/called but I am following the format mentioned in the questions/forums below:
So when I run the test, the assert always fails because the spy isn't working and I get an actual false returned from methodB
I even stepped over the debugger to methodB and I see it being executed. I believe methodB should not have been called at all.