Ad Infinitum Ad Infinitum - 4 months ago 35
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

@Test
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

java.lang.AssertionError:
Unexpected method call List.size():
List.get(1): expected: 1, actual: 0
List.size(): expected: 1, actual: 2
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
at com.sun.proxy.$Proxy7.size(Unknown Source)
at de.psi.passage3.auslieferung.allg.gui.status.CasBarUserConfigurationArrangementsTest.testCreateIdealConf(CasBarUserConfigurationArrangementsTest.java:113)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
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

It looks like you are telling it to expect:

get(0)
get(1)
size()

but the actual method calls would be:

size()
get(0)
size()
get(1)
size()