From 5e5d14e7dd861f6808c28b001504c6e10d9a4cdf Mon Sep 17 00:00:00 2001 From: Muhammad Wahyudin Date: Tue, 18 Jun 2019 15:57:20 +0700 Subject: [PATCH] Submissions 4 Refactor & cleanup viewmodel test Adjust test naming --- .../views/leagues/MatchSearchTest.kt | 3 +- .../kadefootballapp/data/RepositoryTest.kt | 4 +- .../leaguedetail/LeagueDetailViewModelTest.kt | 60 ++++++++++--------- .../matchsearch/MatchSearchViewModelTest.kt | 34 ++++++----- 4 files changed, 55 insertions(+), 46 deletions(-) diff --git a/app/src/androidTest/java/com/muhammadwahyudin/kadefootballapp/views/leagues/MatchSearchTest.kt b/app/src/androidTest/java/com/muhammadwahyudin/kadefootballapp/views/leagues/MatchSearchTest.kt index 0966ce5..cbf7bd6 100644 --- a/app/src/androidTest/java/com/muhammadwahyudin/kadefootballapp/views/leagues/MatchSearchTest.kt +++ b/app/src/androidTest/java/com/muhammadwahyudin/kadefootballapp/views/leagues/MatchSearchTest.kt @@ -26,8 +26,7 @@ import org.junit.Test @LargeTest class MatchSearchTest { - @Rule - @JvmField + @get:Rule var mActivityTestRule = ActivityTestRule(LeaguesActivity::class.java) private val okhttpResources = OkHttp3IdlingResource.create( diff --git a/app/src/test/java/com/muhammadwahyudin/kadefootballapp/data/RepositoryTest.kt b/app/src/test/java/com/muhammadwahyudin/kadefootballapp/data/RepositoryTest.kt index 77194cf..895ecf5 100644 --- a/app/src/test/java/com/muhammadwahyudin/kadefootballapp/data/RepositoryTest.kt +++ b/app/src/test/java/com/muhammadwahyudin/kadefootballapp/data/RepositoryTest.kt @@ -134,7 +134,7 @@ class RepositoryTest { * might fail the test if the data from network changes, or site is down */ @Test - fun get_next_match_by_league_id_from_network_failed() { + fun get_next_match_by_league_id_from_network_empty() { // Given val leagueId = "123" println("League ID: $leagueId") @@ -173,7 +173,7 @@ class RepositoryTest { * might fail the test if the data from network changes, or site is down */ @Test - fun get_last_match_by_league_id_from_network_failed() { + fun get_last_match_by_league_id_from_network_empty() { // Given val leagueId = "123" println("League ID: $leagueId") diff --git a/app/src/test/java/com/muhammadwahyudin/kadefootballapp/views/leaguedetail/LeagueDetailViewModelTest.kt b/app/src/test/java/com/muhammadwahyudin/kadefootballapp/views/leaguedetail/LeagueDetailViewModelTest.kt index 8346669..cd8ce2c 100644 --- a/app/src/test/java/com/muhammadwahyudin/kadefootballapp/views/leaguedetail/LeagueDetailViewModelTest.kt +++ b/app/src/test/java/com/muhammadwahyudin/kadefootballapp/views/leaguedetail/LeagueDetailViewModelTest.kt @@ -2,62 +2,68 @@ package com.muhammadwahyudin.kadefootballapp.views.leaguedetail import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.Observer +import com.jraska.livedata.test import com.muhammadwahyudin.kadefootballapp.data.IRepository import com.muhammadwahyudin.kadefootballapp.data.remote.response.LeagueDetailRes -import org.junit.Assert.* +import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.verify +import com.nhaarman.mockitokotlin2.whenever import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.rules.TestRule import org.mockito.ArgumentMatchers -import org.mockito.Mock -import org.mockito.Mockito -import org.mockito.MockitoAnnotations class LeagueDetailViewModelTest { @get:Rule var rule: TestRule = InstantTaskExecutorRule() - lateinit var viewModel: LeagueDetailViewModel - - @Mock - lateinit var observer: Observer - @Mock - lateinit var repository: IRepository + private lateinit var viewModel: LeagueDetailViewModel + private val repository: IRepository = mock() @Before - fun before() { - MockitoAnnotations.initMocks(this) + fun setUp() { viewModel = LeagueDetailViewModel(repository) } @Test fun getLeagueDetailSuccess() { + // Given val leagueId = 4328 println("League Id: $leagueId") - Mockito.`when`(viewModel.getLeagueDetail(ArgumentMatchers.anyInt())).thenAnswer { - return@thenAnswer MutableLiveData(LeagueDetailRes.League(idLeague = "4328")) - } + whenever(repository.getLeagueDetail(ArgumentMatchers.anyInt())) + .thenAnswer { + MutableLiveData(LeagueDetailRes.League(idLeague = "4328")) + } + // Test val ld = viewModel.getLeagueDetail(leagueId) - ld.observeForever { observer } - Mockito.verify(repository).getLeagueDetail(leagueId) - assertNotNull(ld.value) - assertEquals(leagueId.toString(), ld.value?.idLeague) + // Assert + verify(repository).getLeagueDetail(leagueId) + ld.test() + .assertHasValue() + .assertValue { + it.idLeague == leagueId.toString() + } println("Expected: $leagueId\tActual: ${ld.value?.idLeague}") } @Test - fun getLeagueDetailFailed() { + fun getLeagueDetailNoResult() { + // Given val leagueId = 1234 println("League Id: $leagueId") - Mockito.`when`(viewModel.getLeagueDetail(ArgumentMatchers.anyInt())).thenAnswer { - return@thenAnswer MutableLiveData(null) - } + whenever(repository.getLeagueDetail(ArgumentMatchers.anyInt())) + .thenAnswer { + MutableLiveData(null) + } + // Test val ld = viewModel.getLeagueDetail(leagueId) - ld.observeForever { observer } - Mockito.verify(repository).getLeagueDetail(leagueId) - assertNull(ld.value) + // Assert + verify(repository).getLeagueDetail(leagueId) + ld.test() + .assertValue { + it == null + } println("Expected: null\tActual:${ld.value}") } } \ No newline at end of file diff --git a/app/src/test/java/com/muhammadwahyudin/kadefootballapp/views/matchsearch/MatchSearchViewModelTest.kt b/app/src/test/java/com/muhammadwahyudin/kadefootballapp/views/matchsearch/MatchSearchViewModelTest.kt index 45471fd..ddcad7e 100644 --- a/app/src/test/java/com/muhammadwahyudin/kadefootballapp/views/matchsearch/MatchSearchViewModelTest.kt +++ b/app/src/test/java/com/muhammadwahyudin/kadefootballapp/views/matchsearch/MatchSearchViewModelTest.kt @@ -5,12 +5,10 @@ import androidx.lifecycle.Observer import com.muhammadwahyudin.kadefootballapp.data.IRepository import com.muhammadwahyudin.kadefootballapp.data.model.* import com.muhammadwahyudin.kadefootballapp.data.remote.response.SearchEventsRes -import com.nhaarman.mockitokotlin2.doAnswer import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.whenever import io.reactivex.Single -import io.reactivex.observers.TestObserver import org.junit.Before import org.junit.Rule import org.junit.Test @@ -23,7 +21,6 @@ class MatchSearchViewModelTest { private lateinit var viewModel: MatchSearchViewModel private val repository: IRepository = mock() - private lateinit var searchTestObserver: TestObserver private val stateObserver: Observer>> = mock() @Before @@ -31,34 +28,41 @@ class MatchSearchViewModelTest { viewModel = MatchSearchViewModel(repository).apply { state.observeForever(stateObserver) } - searchTestObserver = TestObserver.create() } @Test fun searchMatchNoResult() { + // Given val query = "milann" - doAnswer { - stateObserver.onChanged(NoResultState(query)) - return@doAnswer Single.just(SearchEventsRes(emptyList())) - }.whenever(repository).searchMatches(query) - verify(stateObserver).onChanged(EmptyState()) + whenever(repository.searchMatches(query)) + .thenAnswer { + stateObserver.onChanged(NoResultState(query)) + Single.just(SearchEventsRes(emptyList())) + } + verify(stateObserver).onChanged(EmptyState()) // Assert nyelip + // Test viewModel.searchMatch(query) // Invoke + // Assert verify(repository).searchMatches(query) verify(stateObserver).onChanged(LoadingState()) verify(stateObserver).onChanged(NoResultState(query)) } @Test - fun searchMatchSuccess() { + fun searchMatchHasResult() { + // Given val query = "milan" val searchEventsRes: SearchEventsRes = mock() val listOfEvents: List = mock() - doAnswer { - stateObserver.onChanged(PopulatedState(listOfEvents)) - Single.just(searchEventsRes) - }.whenever(repository).searchMatches(query) - verify(stateObserver).onChanged(EmptyState()) + whenever(repository.searchMatches(query)) + .thenAnswer { + stateObserver.onChanged(PopulatedState(listOfEvents)) + Single.just(searchEventsRes) + } + verify(stateObserver).onChanged(EmptyState()) // Assert nyelip + // Test viewModel.searchMatch(query) // Invoke + // Assert verify(repository).searchMatches(query) verify(stateObserver).onChanged(LoadingState()) verify(stateObserver).onChanged(PopulatedState(listOfEvents))