title: How to create an observer using Mockk
body: <p>Hello I have a unit test which was using Mockito, I converted most part of it to use Mockk except one part where I want to create a Observer of android lifecycle</p>
  <p>Mockito version which works</p>
  <pre><code>@Mock
  private lateinit var dataObserver: Observer&lt;Result&lt;List&lt;Character&gt;&gt;&gt; 
  </code></pre>
  <p>Mockk version which does not work</p>
  <pre><code>private var dataObserver: Observer&lt;Result&lt;List&lt;Character&gt;&gt;&gt; = mockk(relaxed = true)
  </code></pre>
  <p>My test case fails with the following error when I use Mockk for the above and passes when I use Mockito version</p>
  <p>error message</p>
  <pre><code>java.lang.AssertionError: Verification failed: call 1 of 1: Observer(#2).onChanged(eq(Success([Character(name=myName, img=image, occupation=[], status=status, nickname=nickName, appearance=[])])))) was not called
  
      at io.mockk.impl.recording.states.VerifyingState.failIfNotPassed(VerifyingState.kt:66)
      at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:42)
      at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
      at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
      at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
      at io.mockk.MockKDsl.internalCoVerify(API.kt:143)
      at io.mockk.MockKKt.coVerify(MockK.kt:175)
      at io.mockk.MockKKt.coVerify$default(MockK.kt:172)
      at com.example.breakingbad.MainActivityViewModelTest$fetchCharacters$1.invokeSuspend(MainActivityViewModelTest.kt:76)
  </code></pre>
  <p>Full test case</p>
  <pre><code>package com.example.breakingbad
  
  import androidx.arch.core.executor.testing.InstantTaskExecutorRule
  import androidx.lifecycle.Observer
  import com.example.breakingbad.data.DataRepository
  import com.example.breakingbad.model.Character
  import com.example.breakingbad.viewModel.MainActivityViewModel
  import io.mockk.coEvery
  import io.mockk.coVerify
  import io.mockk.mockk
  import kotlinx.coroutines.Dispatchers
  import kotlinx.coroutines.ExperimentalCoroutinesApi
  import kotlinx.coroutines.launch
  import kotlinx.coroutines.test.*
  import org.junit.After
  import org.junit.Before
  import org.junit.Rule
  import org.junit.Test
  import org.junit.runner.RunWith
  import org.mockito.Mock
  import org.mockito.junit.MockitoJUnitRunner
  
  //@RunWith(MockitoJUnitRunner::class)
  
  @ExperimentalCoroutinesApi
  class MainActivityViewModelTest {
  
      @get:Rule
      val instantTaskExecutorRule = InstantTaskExecutorRule()
  
      private val testDispatcher = TestCoroutineDispatcher()
      private val testCoroutineScope = TestCoroutineScope(testDispatcher)
  
      private val dataRepository: DataRepository = mockk(relaxed = true)
  
      private val mainActivityViewModel = MainActivityViewModel(dataRepository)
  
  //    @Mock
  //    private lateinit var dataObserver: Observer&lt;Result&lt;List&lt;Character&gt;&gt;&gt;
  
      private var dataObserver: Observer&lt;Result&lt;List&lt;Character&gt;&gt;&gt; = mockk(relaxed = true)
  
      @Before
      fun setUp() {
          Dispatchers.setMain(testDispatcher)
      }
  
      @After
      fun cleanup() {
          Dispatchers.resetMain()
          testCoroutineScope.cleanupTestCoroutines()
      }
  
      @Test
      fun fetchCharacters() {
          testCoroutineScope.launch {
  
              coEvery { dataRepository.getCharacters() } returns Result.success(arrayListOf(Character(
                      name = &quot;myName&quot;,
                      img = &quot;image&quot;,
                      occupation = arrayListOf(),
                      status = &quot;status&quot;,
                      nickname = &quot;nickName&quot;,
                      appearance = arrayListOf()
              )))
  
              mainActivityViewModel.fetchCharacters()
              coVerify { dataRepository.getCharacters() }
  
              coVerify { dataObserver.onChanged(
                      Result.success(listOf(Character (
                              name = &quot;myName&quot;,
                              img = &quot;image&quot;,
                              occupation = arrayListOf(),
                              status = &quot;status&quot;,
                              nickname = &quot;nickName&quot;,
                              appearance = arrayListOf()
                      )))
              ) }
              mainActivityViewModel.charactersLiveData.removeObserver(dataObserver)
          }
      }
  }
  </code></pre>
  <p>how to make the test case pass using Mockk? what am I doing wrong?</p>
  <p><strong>Edit</strong></p>
  <p>ViewModel</p>
  <pre><code>class MainActivityViewModel @Inject constructor(
          private val dataRepository: DataRepository
  ): ViewModel() {
      private val _charactersLiveData = MutableLiveData&lt;Result&lt;ArrayList&lt;Character&gt;&gt;&gt;()
      val charactersLiveData: LiveData&lt;Result&lt;ArrayList&lt;Character&gt;&gt;&gt; = _charactersLiveData
  
      fun fetchCharacters() {
          viewModelScope.launch(Dispatchers.IO) {
              _charactersLiveData.postValue(dataRepository.getCharacters())
          }
      }
  }
  </code></pre>