How to verify internal proxy calls in unit testing by using Mockito framework?
I am trying to write a test for doAllTasks()
method and verify that doSingleTask()
was called a certain amount of times. Obviously I can not split my service into two because these methods have the same meaning. The simplest solution is to add setProxy()
method but disadvantage of this is that I will need to add test related code to my service definition. Any ideas?
@Service
public class XXXServiceImpl implements XXXService, BeanNameAware {
private String name;
private XXXService proxy;
@Autowired
private ApplicationContext applicationContext;
@Override
public void setBeanName(String name) {
this.name = name;
}
@PostConstruct
public void postConstruct() {
proxy = (XXXService)applicationContext.getBean(name);
}
@Transactional
public void doAllTasks(){
for(...)
proxy.doSingleTask(...);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void doSingleTask(...){
}
}