Skip to content

Commit

Permalink
Update unit test mocking dataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Jun 14, 2020
1 parent 59596c1 commit 567df60
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/src/test/java/com/skydoves/pokedex/network/ApiAbstract.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.skydoves.pokedex.network

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.skydoves.sandwich.DataSourceCallAdapterFactory
import java.io.IOException
import java.nio.charset.StandardCharsets
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -73,6 +74,7 @@ abstract class ApiAbstract<T> {
return Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(DataSourceCallAdapterFactory())
.build()
.create(clazz)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.nhaarman.mockitokotlin2.whenever
import com.skydoves.pokedex.model.PokemonInfo
import com.skydoves.pokedex.model.PokemonResponse
import com.skydoves.sandwich.ApiResponse
import com.skydoves.sandwich.toResponseDataSource
import java.io.IOException
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.instanceOf
Expand All @@ -44,7 +45,8 @@ class PokedexServiceTest : ApiAbstract<PokedexService>() {
fun fetchPokemonListFromNetworkTest() {
enqueueResponse("/PokemonResponse.json")

val responseBody = requireNotNull(service.fetchPokemonList().execute().body())
val dataSourceCall = requireNotNull(service.fetchPokemonList().toResponseDataSource().call)
val responseBody = requireNotNull(dataSourceCall.execute().body())
mockWebServer.takeRequest()

assertThat(responseBody.count, `is`(964))
Expand Down Expand Up @@ -72,7 +74,8 @@ class PokedexServiceTest : ApiAbstract<PokedexService>() {
fun fetchPokemonInfoFromNetworkTest() {
enqueueResponse("/Bulbasaur.json")

val responseBody = requireNotNull(service.fetchPokemonInfo("bulbasaur").execute().body())
val dataSourceCall = requireNotNull(service.fetchPokemonInfo("bulbasaur").toResponseDataSource().call)
val responseBody = requireNotNull(dataSourceCall.execute().body())
mockWebServer.takeRequest()

assertThat(responseBody.id, `is`(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.skydoves.pokedex.network.PokedexClient
import com.skydoves.pokedex.network.PokedexService
import com.skydoves.pokedex.persistence.PokemonInfoDao
import com.skydoves.pokedex.utils.MockUtil.mockPokemonInfo
import com.skydoves.sandwich.ResponseDataSource
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.Before
Expand Down Expand Up @@ -63,10 +64,12 @@ class DetailRepositoryTest {
@Test
fun fetchPokemonInfoFromNetwork() = runBlocking {
val mockData = mockPokemonInfo()
val dataSourceCall =
ResponseDataSource<PokemonInfo>().combine(getCall(mockData)) {}
whenever(pokemonInfoDao.getPokemonInfo(name_ = "bulbasaur")).thenReturn(null)
whenever(service.fetchPokemonInfo(name = "bulbasaur")).thenReturn(getCall(mockData))
whenever(service.fetchPokemonInfo(name = "bulbasaur")).thenReturn(dataSourceCall)

val loadData = repository.fetchPokemonInfo(name = "bulbasaur") { }
val loadData = repository.fetchPokemonInfo(name = "bulbasaur", onSuccess = {}, onError = {})
verify(pokemonInfoDao, atLeastOnce()).getPokemonInfo(name_ = "bulbasaur")
verify(service, atLeastOnce()).fetchPokemonInfo(name = "bulbasaur")

Expand All @@ -84,10 +87,12 @@ class DetailRepositoryTest {
@Test
fun fetchPokemonInfoFromDatabase() = runBlocking {
val mockData = mockPokemonInfo()
val dataSourceCall =
ResponseDataSource<PokemonInfo>().combine(getCall(mockData)) {}
whenever(pokemonInfoDao.getPokemonInfo(name_ = "bulbasaur")).thenReturn(mockData)
whenever(service.fetchPokemonInfo(name = "bulbasaur")).thenReturn(getCall(mockData))
whenever(service.fetchPokemonInfo(name = "bulbasaur")).thenReturn(dataSourceCall)

val loadData = repository.fetchPokemonInfo(name = "bulbasaur") { }
val loadData = repository.fetchPokemonInfo(name = "bulbasaur", onSuccess = {}, onError = {})
verify(pokemonInfoDao, atLeastOnce()).getPokemonInfo(name_ = "bulbasaur")
verifyNoMoreInteractions(service)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.skydoves.pokedex.network.PokedexClient
import com.skydoves.pokedex.network.PokedexService
import com.skydoves.pokedex.persistence.PokemonDao
import com.skydoves.pokedex.utils.MockUtil.mockPokemonList
import com.skydoves.sandwich.ResponseDataSource
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.Before
Expand Down Expand Up @@ -64,10 +65,12 @@ class MainRepositoryTest {
@Test
fun fetchPokemonListFromNetwork() = runBlocking {
val mockData = PokemonResponse(count = 984, next = null, previous = null, results = mockPokemonList())
val dataSourceCall =
ResponseDataSource<PokemonResponse>().combine(getCall(mockData)) {}
whenever(pokemonDao.getPokemonList(page_ = 0)).thenReturn(emptyList())
whenever(service.fetchPokemonList()).thenReturn(getCall(mockData))
whenever(service.fetchPokemonList()).thenReturn(dataSourceCall)

val loadData = repository.fetchPokemonList(page = 0) { }
val loadData = repository.fetchPokemonList(page = 0, onSuccess = {}, onError = {})
verify(pokemonDao, atLeastOnce()).getPokemonList(page_ = 0)
verify(service, atLeastOnce()).fetchPokemonList()

Expand All @@ -85,10 +88,12 @@ class MainRepositoryTest {
@Test
fun fetchPokemonListFromDatabase() = runBlocking {
val mockData = PokemonResponse(count = 984, next = null, previous = null, results = mockPokemonList())
val dataSourceCall =
ResponseDataSource<PokemonResponse>().combine(getCall(mockData)) {}
whenever(pokemonDao.getPokemonList(page_ = 0)).thenReturn(mockData.results)
whenever(service.fetchPokemonList()).thenReturn(getCall(mockData))
whenever(service.fetchPokemonList()).thenReturn(dataSourceCall)

val loadData = repository.fetchPokemonList(page = 0) { }
val loadData = repository.fetchPokemonList(page = 0, onSuccess = {}, onError = {})
verify(pokemonDao, atLeastOnce()).getPokemonList(page_ = 0)
verifyNoMoreInteractions(service)

Expand Down

0 comments on commit 567df60

Please sign in to comment.