Android Mockito-Kotlin error – “Actually, there were zero interactions with this mock”


The following is the test code for my ViewModel class. My app architecture is based on MVI. Basically, I’m trying to mock my repo and state classes. In the test function I wrote below, I’m trying to check if state is changed in correct order when I successfully load a news list from an API.

class NewsListViewModelTest {

    val testSchedulerRule  RxTestSchedulerRule()

    private lateinit var testSubject: NewsListViewModel    

    private val loadingState  NewsListState(state  State.LOADING)

    private val newsRepo  mock<NewsRepository>()
    private val observer  mock<Observer<NewsListState>>()

    fun setUp() {
        testSubject  NewsListViewModel(newsRepo)

    fun `Given news list successfully loaded, when action LoadNewsList is received, then state contains news list`() {
        // GIVEN
        val newsList  listOf(News("title", "description", Date(), "image"))
        val successState  NewsListState(newsList  newsList, state  State.DATA)

        whenever(newsRepo.loadAll("keyword", 1, 1))

        // WHEN

        // THEN
        inOrder(observer) {

However, when I run this test, I’m getting the following error in the first line inside inOrder(observer){ ... }:

Wanted but not invoked:
    NewsListState(newsList[], stateLOADING, errorMessage)
-> at [packagename].NewsListViewModelTest.Given news list failed to load, when action LoadNewsList is received, then state contains error(NewsListViewModelTest.kt:77)
Actually, there were zero interactions with this mock.

And these are my testing dependencies:

testImplementation 'junit:junit:4.12'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'

What might be the reason of this error?

Edit — My RxTestSchedulerRule class:

class RxTestSchedulerRule(private val testScheduler: TestScheduler  TestScheduler()) : Scheduler(),
    TestRule {
    override fun apply(base: Statement, description: Description?): Statement {
        RxJavaPlugins.setIoSchedulerHandler { testScheduler }
        RxJavaPlugins.setComputationSchedulerHandler { testScheduler }
        RxJavaPlugins.setNewThreadSchedulerHandler { testScheduler }
        RxJavaPlugins.setSingleSchedulerHandler { testScheduler }
        RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
        return base

    override fun createWorker()  testScheduler.createWorker()

    fun triggerActions()  testScheduler.triggerActions()

Edit 2 — My dispatch function:

fun dispatch(?) {
     newsRepository.loadAll(keyword  it.keyword, 
                            pageSize  pageSize, 
                            page  page)
                   .map<NewsListChange> { newsList -> 
                         NewsListChange.Data(newsList) } 
                  .onErrorReturn { throwable -> 
                       NewsListChange.Error(throwable) } 


please try instead of this line

whenever(newsRepo.loadAll("keyword", 1, 1))


whenever(newsRepo.loadAll(anyString(), anyInt(), anyInt()))

Answered By – Lena Bru

Leave a Comment