release 2.1.97
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
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<Repository>()
|
||||
|
||||
// Mock Observer for LiveData
|
||||
@Mock
|
||||
private lateinit var mockObserver: Observer<String>
|
||||
|
||||
// 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<DiagnosticsData>()
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user