user2359634
user2359634

Reputation: 1375

EasyMock java.lang.AssertionError: Unexpected method call

I am new to EasyMock and am stuck now. Can't I set the fields of mocked object? Where am I going wrong? Any help would be really appreciated.

IService service = EasyMock.createMock(IService.class);
service.setName("abc"); 
EasyMock.replay(service);
org.junit.Assert.assertEquals("abc", service.getName());
EasyMock.verify(service);

    java.lang.AssertionError: 
      Unexpected method call getName():
        setName("abc"): expected: 1, actual: 0
        at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:45)
        at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:73)
        at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:92)

Upvotes: 0

Views: 28264

Answers (1)

user2359634
user2359634

Reputation: 1375

I was able to set the field using expect(..) of EasyMock.

Remove this line of code

service.setName("abc");

and add

EasyMock.expect(service.getName()).andReturn("abc");

Upvotes: 4

Related Questions