Skip to content

Instantly share code, notes, and snippets.

@lampard-android
Created November 15, 2021 17:22
Show Gist options
  • Save lampard-android/90ca3dcb7b813d8b3904e0d8162dccb9 to your computer and use it in GitHub Desktop.
Save lampard-android/90ca3dcb7b813d8b3904e0d8162dccb9 to your computer and use it in GitHub Desktop.
ListRestaurantsViewModelTest.kt
package com.codegym.demo.viewmodel
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.codegym.demo.TestMainCoroutineRule
import com.codegym.demo.data.ResponseData
import com.codegym.demo.data.RestaurantItem
import com.codegym.demo.data.RestaurantModel
import com.codegym.demo.list.viewmodel.ListRestaurantsViewModel
import com.codegym.demo.repository.RestaurantRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.runBlockingTest
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import retrofit2.Response
@ExperimentalCoroutinesApi
class ListRestaurantsViewModelTest {
private val testDispatcher = TestCoroutineDispatcher()
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = TestMainCoroutineRule()
lateinit var repositoryMock: RestaurantRepository
@Before
fun setup() {
repositoryMock = Mockito.mock(RestaurantRepository::class.java)
}
@Test
fun getAllRestaurantsSuccess() {
val viewModel = ListRestaurantsViewModel(repositoryMock, testDispatcher)
// GIVEN
val mockData = RestaurantModel(
timestamp = System.currentTimeMillis().toInt(),
restaurants = listOf(
RestaurantItem("Go Noodle House", "Mon-Sun 11:30 am - 9:00 pm"),
RestaurantItem(
"Two Sons Bistro",
"Mon-Tue 11:00 am - 11:00 pm / Sun 11:00 am - 10:00 pm"
),
RestaurantItem(
"The Tokyo Restaurant",
"Mon-Fri 10:00 am - 9:30 pm / Sat-Sun 9:30 am - 9:30 pm"
)
)
)
testDispatcher.runBlockingTest {
// WHEN
whenever(repositoryMock.getAllRestaurants()).thenReturn(Response.success(mockData))
viewModel.getAllRestaurants()
// THEN
val tasks = viewModel.listRestaurantLiveData.value
Assert.assertEquals(tasks, ResponseData.Success(mockData))
}
}
@Test
fun getAllRestaurantsFailure() {
val viewModel = ListRestaurantsViewModel(repositoryMock, testDispatcher)
// GIVEN
val mockError = Response.error<RestaurantModel>(
403,
"{\"errorCode\":[\"Something wrong\"]}"
.toResponseBody("application/json".toMediaTypeOrNull())
)
testDispatcher.runBlockingTest {
// WHEN
whenever(repositoryMock.getAllRestaurants()).thenReturn(mockError)
viewModel.getAllRestaurants()
// THEN
val tasks = viewModel.listRestaurantLiveData.value
Assert.assertTrue(tasks is ResponseData.Error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment