/* * // 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 android.content.Context import android.content.SharedPreferences import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.example.hpostesting.data.dao.HemoCubeDao 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.autodac.AutoDacViewModel import com.example.hpostesting.util.NetworkMonitor import io.mockk.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.TestCoroutineDispatcher import kotlinx.coroutines.test.setMain import org.junit.Before import org.junit.Rule import org.junit.Test @ExperimentalCoroutinesApi class AutoDacViewModelTest { @get:Rule val instantTaskExecutorRule = InstantTaskExecutorRule() private lateinit var viewModel: AutoDacViewModel private val hemoCubeDao = mockk() private val repository = mockk() private val context = mockk() private val sharedPreferences = mockk() private val networkStatusLiveData = mockk() @Before fun setup() { MockKAnnotations.init(this) // Mocking the Context and its dependencies every { context.applicationContext } returns context every { context.getSharedPreferences(any(), any()) } returns sharedPreferences every { context.getSystemService(any()) } returns networkStatusLiveData // Mocking the NetworkStatusLiveData every { networkStatusLiveData.startMonitoring() } just Runs every { hemoCubeDao.getAll() } returns mockk() // every { repository.addDiagnostics(any()) } returns Response.Success(Unit) viewModel = AutoDacViewModel(hemoCubeDao, repository, context) Dispatchers.setMain(TestCoroutineDispatcher()) } @Test fun `test addAutoDacDataToDb success`() = runBlocking { // Given val diagnosticsData = mockk() coEvery { repository.addDiagnostics(diagnosticsData) } returns Response.Success("Success") // When viewModel.addAutoDacDataToDb(diagnosticsData) // Then assert(viewModel.fireBaseUpload.value == "Success") } @Test fun `test addAutoDacDataToDb error`() = runBlocking { // Given coEvery { repository.addDiagnostics(any()) } returns Response.Error(Exception("Test error")) // When viewModel.addAutoDacDataToDb(mockk()) // Then assert(viewModel.fireBaseUpload.value == "Error") } }