Genzer
Genzer

Reputation: 2991

Mocked List using Mockito isEmpty always returns false, even if the size is 0

I'm playing with Mockito (1.9.5) and stuck at first simple test case:

List mockedList = mock(ArrayList.class);
assertEquals(0, mockedList.size()); // Passed
assertTrue(mockedList.isEmpty()); // Failed

Can anyone explain why isEmpty() here returns false while the size() returns 0?

Upvotes: 10

Views: 4602

Answers (1)

bruno conde
bruno conde

Reputation: 48265

I think this happens because mockito doesn't know the semantic meaning of isEmpty() and when it encounters a boolean method mocks it with a default value that is false. Same think happens with size() but the default value here is 0.

Basically, you need to define the expected behaviour of your mocked object. If you don't, it will return default values.

Upvotes: 17

Related Questions