title: How do I mock my repository and apiClient from inside my viewModel
body: <p>I have a <strong>MainViewModel</strong>:</p>
  
  <pre><code> class MainViewModel @Inject constructor(val mainRepository: MainRepository) : ViewModel() {
  .....
  val source: LiveData&lt;GenericApiResponse&lt;GitResult&gt;&gt; = mainRepository.fetchApiresultFromClient(str_query)  
  }
  </code></pre>
  
  <p><strong>Mainrepository:</strong></p>
  
  <pre><code>class MainRepository @Inject constructor(val mainApiClient: MainApiClient) {
      ....
      fun fetchApiresultFromClient(strQuery: String)
       : LiveData&lt;GenericApiResponse&lt;GitResult&gt;&gt; {
  
        var returnedData =    mainApiClient.getApiResultFromServer(strQuery)
        return returnedData
       }
  </code></pre>
  
  <p><strong>MainApiClient:</strong></p>
  
  <pre><code>class MainApiClient  @Inject constructor(val mainApi: MainApi) {
  
     fun getApiResultFromServer(query :String )
          : LiveData&lt;GenericApiResponse&lt;GitResult&gt;&gt; {
  
      var apiResult =   mainApi.getAllRepo(query)
  
         return apiResult
  
      }
  }
  </code></pre>
  
  <p>Now I want to test the below function in viewModel <code>mainRepository.fetchApiresultFromClient(str_query)</code></p>
  
  <p>I want to test that when the above function is called from viewModel then it returns the same result that's returned from <code>mainApiClient.getApiResultFromServer()</code></p>
  
  <p>But to do that I need to mock both repository and apiClient. I tried the below but unfortunately seems its not working.</p>
  
  <pre><code>@ExtendWith(InstantExecutorExtension::class)
  class MainViewModelTest {
  
      lateinit var  mainViewModel: MainViewModel
  
      @Mock
       lateinit var  mainRepository: MainRepository
  
      @Mock
      lateinit var  mainApiClient: MainApiClient
  
  
      @BeforeEach
      fun init()
      {
      MockitoAnnotations.initMocks(this)
      mainViewModel = MainViewModel(mainRepository)
      }
  
      @Test
      fun setSearchQuery_callsRepositoryMethod_returnSuccess() {
  
             //arrange
           mainViewModel.liveResult.value = ApiSuccessResponse(TestUtil.obj_gitresult) as GenericApiResponse&lt;GitResult&gt;
  
          Mockito.`when`(mainApiClient.getApiResultFromServer(ArgumentMatchers.anyString(), ArgumentMatchers.anyInt()))
              .thenReturn(mainViewModel.liveResult)
  
  
             //act
  
          var repo_response = mainRepository.fetchApiresultFromClient("java")
  
  
          //assert
             // assertTrue(repo_response.value is  GenericApiResponse&lt;GitResult&gt; ) //for asserting return type is of type GenericResponse //--TestResult- java.lang.NullPointerException
             // assertTrue(repo_response.value is  ApiSuccessResponse&lt;GitResult&gt; ) //for asserting return type is of type ApiSuccessResponse //--TestResult- java.lang.NullPointerException
             //  assertEquals(mainViewModel.liveResult.value , repo_response.value ) //for asserting 2 generic types are equal //--TestResult- java.lang.NullPointerException
          }
  
  }
  </code></pre>
  
  <p>Am I doing something wrong by mocking both <code>MainRepository</code> and <code>MainApiClient</code>. What is the proper way to do it?</p>