I am trying to get Spring to inject EasyMock mocks in my unit tests.
In my applicationContext.xml, I have this:
<bean id="mockService" class="org.easymock.EasyMock" factory-method="createMock" name="MockService">
<constructor-arg index="0" value="my.project.Service"/>
</bean>
In my unit test I have this:
@Autowired
@Qualifier("mockService")
private Service service;
public void testGetFoo() {
Foo foo = new Foo();
expect(service.findFoo()).andReturn(foo);
replay(service); // <-- This is line 45, which causes the exception
// Assertions go here...
}
When I try to run my test, I get this stack trace:
java.lang.ClassCastException: org.springframework.aop.framework.JdkDynamicAopProxy
at org.easymock.EasyMock.getControl(EasyMock.java:1330)
at org.easymock.EasyMock.replay(EasyMock.java:1279)
at TestFooBar.testGetFoo(TestVodServiceLocator.java:45)
I am quite new to both Spring and EasyMock, but it seems to me that the error is caused by EasyMock trying to call a method on what it assumes to be an instance of EasyMock, but is in reality a dynamic proxy created by Spring. As I understand it, dynamic proxies only implement the methods defined in the interface, in this case the interface for Service.
What I don't understand is that from what I read (also here), what I'm trying to achieve at least seems to be possible.
So my question is: What I'm I not doing or what am I doing wrong?