Skip to content

Instantly share code, notes, and snippets.

@tolmachevroman
Last active November 26, 2017 05:10
Show Gist options
  • Save tolmachevroman/804e9cce0e44b45c4648c6cd423e4687 to your computer and use it in GitHub Desktop.
Save tolmachevroman/804e9cce0e44b45c4648c6cd423e4687 to your computer and use it in GitHub Desktop.
Medium Post 3. RestaurantsViewModelTest
@RunWith(JUnit4::class)
class RestaurantsViewModelTest {
@Rule
@JvmField
var instantExecutor = InstantTaskExecutorRule()
@Mock
lateinit var repository: RestaurantsRepository
@Mock
lateinit var observer: Observer<Resource<List<Restaurant>>>
lateinit var restaurantsViewModel: RestaurantsViewModel
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
restaurantsViewModel = RestaurantsViewModel(repository)
}
/**
* When cuisineInput is set to 1, return all restaurants where cuisine param is 1
*/
@Test
fun getRestaurantsByCuisineFromRepository() {
val restaurant1 = Restaurant(1, 1, "Fake Restaurant", 0.0, 0.0, 1000,
"image", "description")
val restaurant3 = Restaurant(3, 1, "Fake Restaurant 3", 0.0, 0.0, 1000,
"image", "description")
val restaurants = MutableLiveData<Resource<List<Restaurant>>>()
val resource = Resource.success(listOf(restaurant1, restaurant3))
restaurants.value = resource
Mockito.`when`(repository.getRestaurants(1)).thenReturn(restaurants)
restaurantsViewModel.restaurants.observeForever(observer)
Assert.assertFalse(restaurantsViewModel.initialized)
restaurantsViewModel.cuisineInput.value = 1
Assert.assertTrue(restaurantsViewModel.initialized)
verify(observer).onChanged(ArgumentMatchers.refEq(resource))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment