Wednesday, March 27, 2013

EasyMock with Spring Beans (Spring Integration)

Creating mock beans in spring is really easy.

All you have to do is to declare a bean with easymock and set the interface you want to mock as the constructor.

<bean id="somethingCool" class="org.eashmock.EasyMock" factory-method="createMock" primary="true">
   <constructor-arg value="com.mycompany.cool.SomethingCool/>
</bean>

and in the test, just autowired the bean and call expect & reply as usual.

This blog by DevGrok has detailed how that's done and more.

And when I'm try to mock an Spring Integration Gateway only to find that if the gateway is annotated with @Gateway, it can't be mocked.  Spring Integration somehow taken over the proxy and when I call, EasyMock.expect( someGateway.someMethod() ), it actually call Spring Integration and hung waiting for a reply message. I ended up mocking one of the service activator endpoints called by the gateway.