Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit Easymock Unexpected method call

I'm trying to setup a test in JUnit w/ EasyMock and I'm running into a small issue that I can't seem to wrap my head around. I was hoping someone here could help.

Here is a simplified version of the method I'm trying to test:

public void myMethod() {
    //(...)
    Obj myObj = this.service.getObj(param);
    if (myObj.getExtId() != null) {
      OtherObj otherObj = new OtherObj();
      otherObj.setId(myObj.getExtId());
      this.dao.insert(otherObj);
    }
    //(...)
}

Ok so using EasyMock I've mocked the service.getObj(myObj) call and that works fine.

My problem comes when JUnit hits the dao.insert(otherObj) call. EasyMock throws a *Unexpected Method Call* on it.

I wouldn't mind mocking that dao in my test and using expectLastCall().once(); on it, but that assumes that I have a handle on the "otherObj" that's passed as a parameter at insert time... Which of course I don't since it's conditionally created within the context of the method being tested.

Anyone has ever had to deal with that and somehow solved it?

Thanks.

like image 528
Lancelot Avatar asked Jan 21 '10 23:01

Lancelot


People also ask

Does EasyMock support JUnit 5?

Finally, since EasyMock 4.1, JUnit 5 extensions are supported. The annotation has an optional element, 'type', to refine the mock as a 'nice' mock or a 'strict' mock. Another optional annotation, 'name', allows setting of a name for the mock that will be used in the mock () call, which will appear in expectation failure messages for example.

What happens if an unexpected method is called on a mock?

If an unexpected method is called on a strict Mock Object, the message of the exception will show the method calls expected at this point followed by the first conflicting one. verify (mock) shows all missing method calls.

What is expectlastcall () In EasyMock?

With expect (…), EasyMock is expecting the method to return a value or throw an Exception. EasyMock will complain about this, as it requires a call on expect (…).andReturn (…) if the method returns anything. If it's a void method, we can expect its action using expectLastCall () like this: 5.2. Replay Order

How do I check the Order of method calls In EasyMock?

On a Mock Object returned by a EasyMock.mock (), the order of method calls is not checked. If you would like a strict Mock Object that checks the order of method calls, use EasyMock. strict Mock () to create it.


2 Answers

You could also use EasyMock.isA(OtherObj.class) for a little more type safety.

like image 149
dplass Avatar answered Oct 02 '22 18:10

dplass


If you can't get a reference to the object itself in your test code, you could use EasyMock.anyObject() as the expected argument to yourinsert method. As the name suggests, it will expect the method to be called with.. well, any object :)

It's maybe a little less rigorous than matching the exact argument, but if you're happy with it, give it a spin. Remember to include the cast to OtherObjwhen declaring the expected method call.

like image 20
DoctorRuss Avatar answered Oct 02 '22 19:10

DoctorRuss