Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Spring to inject EasyMock mocks causes ClassCastException
    text
    copied!<p>I am trying to get Spring to inject EasyMock mocks in my unit tests.</p> <p>In my applicationContext.xml, I have this:</p> <pre><code>&lt;bean id="mockService" class="org.easymock.EasyMock" factory-method="createMock" name="MockService"&gt; &lt;constructor-arg index="0" value="my.project.Service"/&gt; &lt;/bean&gt; </code></pre> <p>In my unit test I have this:</p> <pre><code>@Autowired @Qualifier("mockService") private Service service; public void testGetFoo() { Foo foo = new Foo(); expect(service.findFoo()).andReturn(foo); replay(service); // &lt;-- This is line 45, which causes the exception // Assertions go here... } </code></pre> <p>When I try to run my test, I get this stack trace:</p> <pre><code>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) </code></pre> <p>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.</p> <p>What I don't understand is that <a href="http://javadevelopmentforthemasses.blogspot.com/2008/07/mocking-spring-tests.html" rel="nofollow noreferrer">from what I read</a> (also <a href="http://satukubik.com/2007/12/21/spring-tips-initializing-bean-using-easymock-for-unit-test/" rel="nofollow noreferrer">here</a>), what I'm trying to achieve at least seems to be possible.</p> <p>So my question is: What I'm I not doing or what am I doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload