Let's say we are writing unit tests for a CustomerController class that uses an OrderService class to post a Customer's Orders:
// OrderService.java
|
In the real world, OrderService probably connects to external resources and therefore we want to mock it when creating a unit test for CustomerController. The interaction between CustomerController and OrderService consists of a single call to the OrderService.postOrders(List
public class TestCustomerController {
|
When you run this, it blows up with the following exception:
java.lang.IllegalStateException: missing behavior definition for the preceding method call postOrders([Order@82c01f]) at org.easymock.internal.MocksControl.replay(MocksControl.java:174) at org.easymock.EasyMock.replay(EasyMock.java:1970) at TestCustomerController.testPostOrders(TestCustomerController.java:22)Since I gave away the punch line in the opening paragraph of this post, you know that this happens because OrderService.postOrders() is not a void method; rather, it returns an int. I would argue that this is a pretty easy mistake to make, though, because the implementation of CustomerController does not care about or capture this return value. So unless we're paying pretty close attention to the OrderService interface, we might assume it returns void. And unfortunately, EasyMock requires that ALL non-void method calls on mocks have return values specified, even if the class under test will ignore it.
So the correct way to set up the OrderService.postOrders() expectation is as follows:
EasyMock.expect(mockOrderService.postOrders(expectedOrders)).andReturn(1);
|
In my next "EasyMock Issues" post, I will talk about a couple of things to look for when your expectations are mysteriously failing to match.
1 comment:
Dear Scott,
Thank you for your post. It comes up in my google search results if I look for EasyMock and "missing behavior definition". I got the missing behavior definition exception you describe here, but it had a different cause. It took me a long time to debug my problem, and therefore I would like to share my solution with your readers.
I have a test case involving many mocks and I use EasyMockSupport to replay and verify all my mocks at once. My test cases have a member variable support of type EasyMockSupport, and my test methods include the call support.replayAll(). Then in the tear down method I have support.verifyAll().
However, I created my mocks like someMock = EasyMock.createStrictMock(Xxx.class). This is wrong. It should be support.createStrictMock(Xxx.class). You should have the EasyMockSupport instance create your mocks if you use the EasyMockSupport instance to manipulate them.
With kind regards,
Martijn Dirkse
mhdirkse@live.nl
Post a Comment