-
Notifications
You must be signed in to change notification settings - Fork 9
/
RetroCoroutineViewModelTest.kt
119 lines (90 loc) · 3.73 KB
/
RetroCoroutineViewModelTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.rxretrofit
import android.util.Log
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.rxretrofit.model.RetroRxModel
import com.rxretrofit.viewmodel.APIService
import com.rxretrofit.viewmodel.RetroCoroutineViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.Unconfined
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.mockito.junit.MockitoJUnitRunner
import retrofit2.Response
import retrofit2.Retrofit
import kotlin.math.exp
@RunWith(MockitoJUnitRunner::class)
@ExperimentalCoroutinesApi
class RetroCoroutineViewModelTest {
@get:Rule
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
@get:Rule
val testCoroutineRule = TestCoroutineRule()
@Mock
lateinit var retrofit: Retrofit
@Mock
lateinit var apiService: APIService
lateinit var retroCoroutineViewModel: RetroCoroutineViewModel
private lateinit var response: Response<List<RetroRxModel>>
private var loading: Boolean = false
/* @Mock
private lateinit var apiUsersObserver: Observer<List<RetroRxModel>>
*/
@Before
fun setUp(){
MockitoAnnotations.initMocks(this)
}
/* @Test
fun fetchRetroInfoTest_success(){
testCoroutineRule.runBlockingTest {
retroCoroutineViewModel = RetroCoroutineViewModel(TestCoroutineDispatcher(),apiService)
var retroRxModel = RetroRxModel("tile", "body", "1")
var retroRXModelList = ArrayList<RetroRxModel>()
retroRXModelList.add(retroRxModel)
response = Response.success(retroRXModelList)
if (retrofit != null) {
if (apiService != null) {
Mockito.`when`(apiService.fetchUserPosts()).thenReturn(response)
retroCoroutineViewModel.fetchResponseFromAPI()
}
}
val funResponse = retroCoroutineViewModel.fetchPostLiveData()
println("Response::::${funResponse.value}")
// Assert.assertNotNull(funResponse.value)
Assert.assertEquals(1,funResponse.value?.size)
Assert.assertEquals(loading,retroCoroutineViewModel.loading)
// Assert.assertEquals(funResponse.value?.size,100)
}
}*/
val testDispatcher = TestCoroutineDispatcher()
@Test
fun `check viewmodel fetches data correctly`() = testDispatcher.runBlockingTest{
var retroRxModel = RetroRxModel("tile", "body", "1")
var retroRXModelList = ArrayList<RetroRxModel>()
retroRXModelList.add(retroRxModel)
response = Response.success(retroRXModelList)
retroCoroutineViewModel = RetroCoroutineViewModel(testDispatcher,apiService)
if (retrofit != null) {
if (apiService != null) {
Mockito.`when`(apiService.fetchUserPosts()).thenReturn(response)
}
}
retroCoroutineViewModel.fetchResponseFromAPI()
// advanceTimeBy(15_000)
println("Loading Val::::${retroCoroutineViewModel.fetchLoadStatus()?.value}")
println("PostLive Dat::::${retroCoroutineViewModel.fetchPostLiveData().value}")
Assert.assertEquals(1,retroCoroutineViewModel.fetchPostLiveData().value?.size)
Assert.assertEquals(false, retroCoroutineViewModel.loading?.value)
}
}