How to use Mockito with Spring-AOP-Proxies

535 views
Skip to first unread message

andreas...@web.de

unread,
May 29, 2015, 11:18:34 AM5/29/15
to moc...@googlegroups.com
Hello,

I would like to use Mockito in an application where I make heavy use of Spring AOP proxies (in our case JDKDynamicProxies). In my test driver, I let Spring autowire the class that is to be tested and use the @Mock und @InjectMocks annotations to prepare it for my JUnit tests. Unfortunately the injection process of @InjectMocks fails when applied to a Spring-injected JDKDynamicProxy instead of the original Implementation.

Through some debugging I found out, that Mockito seems to be not aware of proxies. The  PropertyAndSetterInjection class scans for fields in the proxy and - as there are none - does nothing. Desired behaviour would be for it to detect the proxy, access the target source and inject the mocks there. In order to fix this, I copied the PropertyAndSetterInjection and added some code (4 simple lines) to do exactly that. For now, this solves my problem, but I am somewhat unhappy with the solution. So now to my questions:
  1. Using Mockito with Spring must be a very common scenario. Therefore I assume there must be a way to handle AOP proxies already. I could not find anything about this issue in the faq. What is the point I am missing here?
  2. Instead of patching Mockito, I would have liked to extend it. But I hardly found any defined extension points or configuration options. What would be the best way to provide my own AOPAwarePropertyAndSetterInjection without replacing the whole AnnotationEngine to begin with?
Thanks in advance,

Andreas

Reply all
Reply to author
Forward
0 new messages