Ad Infinitum Ad Infinitum - 1 year ago 253
Java Question

Create a list with EasyMock

I am currently learning EasyMock and I have read some tutorials about it. Based on the knowledge, I have acquired, I try to create a mock list but it gives me AssertionError, whose reason I could not understand.

Basically, I want to have a list, whose first element is 1133L and second element is 1139L and the size of it is naturally 2.

My method

public void testCreateIdealConf()

List< Long > idList = createMock( List.class );

expect( idList.get( 0 ) ).andReturn( 1133L );
expect( idList.get( 1 ) ).andReturn( 1139L );
expect( idList.size() ).andReturn( 2 );

replay( idList );

for( int i = 0; i < idList.size(); i++ )
System.out.println( "Elements: " + idList.get( i ) );

When I run this test method, it gives the following error

Unexpected method call List.size():
List.get(1): expected: 1, actual: 0
List.size(): expected: 1, actual: 2
at org.easymock.internal.MockInvocationHandler.invoke(
at org.easymock.internal.ObjectMethodsFilter.invoke(
at com.sun.proxy.$Proxy7.size(Unknown Source)
at de.psi.passage3.auslieferung.allg.gui.status.CasBarUserConfigurationArrangementsTest.testCreateIdealConf(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
the rest of the failure trace is omitted.

Where do I make a mistake or do I except something wrong from a mock object/list?

Answer Source

It looks like you are telling it to expect:


but the actual method calls would be:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download