EasyMock: missing behavior definition for the preceeding method call

If a method of a class which will be mocked by EasyMock did return some value, you should specify the return value explicitly before you use the mock object in your unit test case. Or else EasyMock will complain:

 

java.lang.IllegalStateException: missing behavior definition for the preceeding method call xxx

 

An example:

Say we have a interface named Subject which has a member method as follows:

 

public interface Subject { Callback addObserver(Callback newObserver); }  

 

Then we should use EasyMock to mock this interface in following way:

 

public void testRegisterObserver() throws Exception { Subject mockSubject = EasyMock.createNiceMock(Subject.class); Callback mockCallback = EasyMock.createNiceMock(Callback.class); EasyMock.expect(mockSubject.addObserver(EasyMock.notNull())).andReturn(mockCallback); EasyMock.replay(mockSubject); // Test code here... EasyMock.verify(mockSubject); } 

 

Note the line contains andReturn. If you missed the call to andReturn, EasyMock will throw out the above IllegalStateException.

 

 

你可能感兴趣的:(behavior,callback,interface,exception,object,class)