Reputation: 11329
I have the following test setup:
class RepositoryTest {
private lateinit var repository: Repository
@Before
fun setup() {
repository = Mockito.mock(Repository::class.java)
Mockito.`when`(repository.getList()).thenReturn(getMockedList())
}
// works
@Test
@Throws(Exception::class)
fun getList() {
val list = repository.getList()
assertNotNull(list)
assertFalse(list.isEmpty())
}
// does not work
@Test
@Throws(Exception::class)
fun getList() {
// getFilteredList is internally using getList()
val list = repository.getFilteredList()
assertNotNull(list)
assertFalse(list.isEmpty())
}
}
So my question is, does the mocking of the return type for getList not work for implicit method calls? And what would be the appropriate way to mock these implicit method calls? getFilteredList
Upvotes: 0
Views: 683
Reputation: 152817
That's how mocks work.
Your repository
is a mock object and does not contain any actual code. By default, for all methods that return a value, a mock will return either null, a primitive/primitive wrapper value, or an empty collection. Since getFilteredList()
is not stubbed, you're getting a default return value that does not pass the assertions you have later.
You can make the mock call your actual method with something like
when(repository.getFilteredList()).thenCallRealMethod()
Upvotes: 2