Skip to content

Commit

Permalink
Submissions 4
Browse files Browse the repository at this point in the history
Refactor & cleanup viewmodel test
Adjust test naming
  • Loading branch information
hyuwah authored and Muhammad Wahyudin committed Jun 18, 2019
1 parent fc9d14a commit 5e5d14e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LeagueDetailRes.League>
@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>(LeagueDetailRes.League(idLeague = "4328"))
}
whenever(repository.getLeagueDetail(ArgumentMatchers.anyInt()))
.thenAnswer {
MutableLiveData<LeagueDetailRes.League>(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<LeagueDetailRes.League>(null)
}
whenever(repository.getLeagueDetail(ArgumentMatchers.anyInt()))
.thenAnswer {
MutableLiveData<LeagueDetailRes.League>(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}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,42 +21,48 @@ class MatchSearchViewModelTest {

private lateinit var viewModel: MatchSearchViewModel
private val repository: IRepository = mock()
private lateinit var searchTestObserver: TestObserver<SearchEventsRes>
private val stateObserver: Observer<ResourceState<List<EventWithImage>>> = mock()

@Before
fun setUp() {
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<EventWithImage> = 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))
Expand Down

0 comments on commit 5e5d14e

Please sign in to comment.