[ACCEPTED]-JUnit Easymock Unexpected method call-easymock

Accepted answer
Score: 14

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

Score: 9

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

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

Score: 5

The anyObject() matcher works great if you 7 just want to get past this call, but if 6 you actually want to validate the constructed 5 object is what you thought it was going 4 to be, you can use a Capture. It would 3 look something like:

Capture<OtherObj> capturedOtherObj = new Capture<OtherObj>();
mockDao.insert(capture(capturedOtherObj));
replay(mockDao);

objUnderTest.myMethod();

assertThat("captured what you expected", capturedOtherObj.getValue().getId(), 
           equalTo(expectedId));

Also, PowerMock has the ability 2 to expect an object to be constructed, so 1 you could look into that if you wanted.

Score: 1

Note also that if you use EasyMock.createStrictMock();, the order of 3 the method calls is also important and if 2 you break this rule, it would throw an unexpected 1 method call.

More Related questions