/* * // Copyright (c) 2024 ShanMukha Innovations Pvt. Ltd. All rights reserved. * // Notice: All information contained herein is, and remains * // the property of ShanMukha Innovations Pvt. Ltd. and its suppliers, * // if any. The intellectual and technical concepts contained * // herein are proprietary to ShanMukha Innovations Pvt. Ltd. * // and its suppliers and may be covered by Indian and Foreign Patents, * // patents in process, and are protected by trade secret or copyright law. * // Dissemination of this information or reproduction of this material * // is strictly forbidden unless prior written permission is obtained * // from ShanMukha Innovations Pvt. Ltd. */ package com.example.hpostesting import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.lifecycle.Observer import com.example.hpostesting.data.model.Response import com.example.hpostesting.data.model.diagnostics.DiagnosticsData import com.example.hpostesting.data.repository.Repository import com.example.hpostesting.presentation.diagnostics.DiagnosticsViewModel import io.mockk.coEvery import io.mockk.mockk import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.TestCoroutineDispatcher import kotlinx.coroutines.test.TestCoroutineScope import kotlinx.coroutines.test.runBlockingTest import kotlinx.coroutines.test.setMain import org.junit.Before import org.junit.Rule import org.junit.Test import org.mockito.Mock import org.mockito.MockitoAnnotations @ExperimentalCoroutinesApi class DiagnosticsViewModelTest { // Use this rule to swap the background executor used by the Architecture Components @get:Rule var instantTaskExecutorRule = InstantTaskExecutorRule() // Use this dispatcher to control execution of coroutines in tests private val testDispatcher = TestCoroutineDispatcher() // Use this scope to control the execution of coroutines in tests private val testCoroutineScope = TestCoroutineScope(testDispatcher) // Mock Repository private val mockRepository = mockk() // Mock Observer for LiveData @Mock private lateinit var mockObserver: Observer // Subject under test private lateinit var diagnosticsViewModel: DiagnosticsViewModel @Before fun setup() { // Initialize mocks MockitoAnnotations.openMocks(this) // Create the ViewModel with the mock repository and set the coroutine dispatcher diagnosticsViewModel = DiagnosticsViewModel(repository = mockRepository) // diagnosticsViewModel.viewModelScope = testCoroutineScope // Observe the LiveData // diagnosticsViewModel.fireBaseUpload.observeForever(mockObserver) Dispatchers.setMain(TestCoroutineDispatcher()) } @Test fun `test addDiagnosticsDataToDb success`() = runBlocking { // Given val diagnosticsData = mockk() coEvery { mockRepository.addDiagnostics(diagnosticsData) } returns Response.Success("Success") // When diagnosticsViewModel.addDiagnosticsDataToDb(diagnosticsData) // Then assert(diagnosticsViewModel.fireBaseUpload.value == "Success") } @Test fun `addDiagnosticsDataToDb error`() = testCoroutineScope.runBlockingTest { // Given coEvery { mockRepository.addDiagnostics(any()) } returns Response.Error(Exception("Test error")) // When diagnosticsViewModel.addDiagnosticsDataToDb(mockk()) // Then assert(diagnosticsViewModel.fireBaseUpload.value == "Error") } }