/* * // 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 androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.lifecycle.Observer import com.example.hpostesting.util.Result import com.example.hpostesting.data.model.login.LoginResponse import com.example.hpostesting.data.repository.Repository import com.example.hpostesting.domain.LogFileManager import com.example.hpostesting.presentation.trueheme_test.TrueHemeTestViewModel import com.example.hpostesting.util.TestCoroutineRule import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.mockk import io.mockk.verify import junit.framework.TestCase import kotlinx.coroutines.ExperimentalCoroutinesApi import org.junit.After import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(manifest = Config.NONE) @ExperimentalCoroutinesApi class HemocubeViewModelTest { @get:Rule val instantTaskExecutorRule = InstantTaskExecutorRule() @get:Rule val coroutineRule = TestCoroutineRule() @MockK(relaxed = true) lateinit var repository: Repository @MockK(relaxed = true) lateinit var logFileManager: LogFileManager @MockK(relaxed = true) lateinit var context: Context @MockK(relaxed = true) lateinit var observer: Observer> private lateinit var viewModel: TrueHemeTestViewModel @Before fun setup() { // MockKAnnotations.init(this) // // val config = Configuration.Builder() // .setMinimumLoggingLevel(android.util.Log.DEBUG) // .setExecutor(SynchronousExecutor()) // .build() // // WorkManagerTestInitHelper.initializeTestWorkManager(context, config) // // every { context.getSharedPreferences(any(), any()) } returns mockk() // // viewModel = HemoCubeViewModel( // mockk(), // mockk(), // repository, // logFileManager, // mockk(), // context // ) // viewModel.loginResponse.observeForever(observer) } @After fun teardown() { // viewModel.loginResponse.removeObserver(observer) } @Test fun `testAddNumbers in HemocubeViewModelTest`() { // Create a mock of the Calculator class using Mockito-Kotlin val calculatorMock = mockk() // Define the behavior of the add method on the mock every { calculatorMock.add(2, 3) } returns 5 // Create an instance of MathApplication with the mock Calculator val mathApplication = MathApplication(calculatorMock) // Perform the test using the MathApplication val result = mathApplication.addNumbers(2, 3) // Verify that the add method of the mock was called with the correct parameters verify { calculatorMock.add(2, 3) } // Verify the result of the test TestCase.assertEquals(5, result) } // @Test // fun `login() should update LiveData with success`() = coroutineRule.runBlockingTest { // // // Arrange // coEvery { repository.login(any()) } returns Result.Success(mockk()) // // // Act // viewModel.login(mockk()) // // // Assert // coVerify { repository.login(any()) } // verify { observer.onChanged(Result.Success(any())) } // } // // @Test // fun `login() should update LiveData with error`() = coroutineRule.runBlockingTest { // // Arrange // coEvery { repository.login(any()) } returns Result.Error(Exception("Login failed")) // // // Act // viewModel.login(mockk()) // // // Assert // coVerify { repository.login(any()) } // verify { observer.onChanged(Result.Error(any())) } // } // // @Test // fun `uploadLogs() should update LiveData with success`() = coroutineRule.runBlockingTest { // // Arrange // val file = mockk(relaxed = true) // val response = Result.Success(mockk()) // every { logFileManager.createLogFile() } returns file // coEvery { repository.uploadLogs(any()) } returns response // // // Act // viewModel.uploadLogs() // // // Assert // verify { observer.onChanged(response as Result) } // } // // @Test // fun `uploadLogs() should update LiveData with error`() = coroutineRule.runBlockingTest { // // Arrange // val file = mockk(relaxed = true) // val response = Result.Error(Exception("Upload failed")) // every { logFileManager.createLogFile() } returns file // coEvery { repository.uploadLogs(any()) } returns response // // // Act // viewModel.uploadLogs() // // // Assert // verify { observer.onChanged(response) } // } }