Sara
Sara

Reputation: 2505

Calling the mocked object inside a for loop using easymock

I have mocked an object using Easymock.createNiceMock.

MYRepository mockedMyRepository = EasyMock.createNiceMock(MyRepositoryWrapper.class);

EasyMock.expect(mockedRepository.findList(Asset.class, criteria)).andReturn(statusTypeList);

In the test class I have a for loop, inside which I call that findList method. It gets the value in the first loop but from the next one it returns null.

Even without a loop, only once it returns the value I specified.

System.out.println("first"+this.myRepository.findList(Asset.class).get(0)); \\ returns the correct value
System.out.println("second"+this.myRepository.findList(Asset.class).get(0)); \\ returns null

What is the solution here? Thanks.

Upvotes: 1

Views: 2433

Answers (1)

John Watts
John Watts

Reputation: 8875

Add

.anyTimes()

to the end of the expectation. So you get

EasyMock.expect(mockedRepository.findList(Asset.class , criteria)).andReturn(statusTypeList).anyTimes();

Otherwise, you are implicitly saying to expect it once only.

Upvotes: 4

Related Questions