I'm a Java software developer, consultant and architect that focuses on enterprise applications, quality and performance. I gained interest in Java due to it's open nature and community support. Next to Java, I spend most of my time trying to stay up to date with everything that moves inside the software world like Scala and NoSQL db's. Jelle is a DZone MVB and is not an employee of DZone and has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

Return the same object that was given as parameter with EasyMock

07.29.2011
| 7422 views |
  • submit to reddit

Every time I have to use EasyMock, I get annoyed. For those who don’t know EasyMock, it is a mocking framework that allows you to mock certain pieces of code when doing unit testing. In my humble opinion, there are better ways to create mocks in unit test, but at my current project I’m using EasyMock.
There are 2 reason, the first is the lack of more advanced mocking tutorials on the interwebs. When I go looking for a specific solution, I never really find what I need.
The second reason is a more justified one, but still personal. I alway forget how the API of EasyMock works, it is not self explanatory, a former colleague of mine even once called it DifficultMock…

I’m not going to rant about my lack of love for this java mocking framework, instead I’m going to offer a solution.
So basically, what I was trying to do, was to capture an argument I give to a method and return the same object. We could take the JPA method EntityManager.merge(T entity) method as example. We take an unattached object and attach it. The result is our attached object. Seems releasable to include in a test.
So I want to capture my argument and return it. After playing a bit with the API I got this

EntityManager entityManager = EasyMock.createNiceMock(EntityManager.class);
final Capture<Entity> capture = new Capture<Entity>();
EasyMock.expect(entityManager.merge(EasyMock.capture(capture))).andReturn(capture.getValue());
replay(entityManager);
executeTest();

This compiled fine, but gave an error at runtime, saying

Nothing captured yet

So I’m not able to define my andReturn method, because it already tries get the value of my captured object. Instead of giving my andReturn method a value, I have to give it a function, a closure. Since Java doesn’t have closures (yet), I’m forced to use a poor mans closure.
EasyMock supplies an Interface and a method we can use here. The IAnwser interface and we replace the andReturn method by the andAnwser method.
By creating an annonymous implementation of this interface, we create a one-function class, like a normal closure.

final EntityManager entityManager = EasyMock.createNiceMock(EntityManager.class);
final Capture<Entity> capture = new Capture<Entity>();
EasyMock.expect(entityManager.merge(EasyMock.capture(capture))).andAnswer(new IAnswer<Entity>() {
 
    public Entity answer() throws Throwable {
        return capture.getValue();
    }
});
replay(entityManager);

 

From http://styledideas.be/blog/2011/07/28/return-the-same-object-that-was-given-as-parameter-with-easymock/

Published at DZone with permission of Jelle Victoor, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)