forked from cobeisfresh/CleanArchitecture-Android-Showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
514d2b8
commit aabce16
Showing
4 changed files
with
54 additions
and
4 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
data/src/test/java/com/example/data/WeatherRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.data | ||
|
||
import com.example.data.database.dao.WeatherDao | ||
import com.example.data.networking.WeatherApi | ||
import com.example.data.repository.weather.WeatherRepositoryImpl | ||
import com.example.data.utils.* | ||
import com.nhaarman.mockitokotlin2.* | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Test | ||
import retrofit2.Response | ||
|
||
class WeatherRepositoryTest { | ||
|
||
private val weatherTestApi: WeatherApi = mock() | ||
private val weatherDao: WeatherDao = mock() | ||
private val weatherRepository = WeatherRepositoryImpl(weatherTestApi, weatherDao) | ||
|
||
@Test | ||
fun `test getWeatherForLocation calls api and saves data to db upon success`() { | ||
runBlocking { | ||
whenever(weatherTestApi.getWeatherForLocation(OSIJEK_CITY_NAME)).thenReturn(Response.success(successWeatherInfoResponse)) | ||
whenever(weatherDao.updateWeatherAndReturn(successWeatherInfoResponse.mapToRoomEntity())).thenReturn(fakeWeatherEntity) | ||
weatherRepository.getWeatherForLocation(OSIJEK_CITY_NAME) | ||
verify(weatherTestApi, times(1)).getWeatherForLocation(OSIJEK_CITY_NAME) | ||
verify(weatherDao, times(1)).updateWeatherAndReturn(fakeWeatherEntity) | ||
} | ||
} | ||
|
||
@Test | ||
fun `test getWeatherForLocation calls api and returns cached data from db upon failure`() { | ||
runBlocking { | ||
whenever(weatherTestApi.getWeatherForLocation(OSIJEK_CITY_NAME)) | ||
.thenReturn(Response.error(FAKE_FAILURE_ERROR_CODE, failureResponseBody)) | ||
weatherRepository.getWeatherForLocation(OSIJEK_CITY_NAME) | ||
verify(weatherTestApi, times(1)).getWeatherForLocation(OSIJEK_CITY_NAME) | ||
// verify(weatherDao, times(1)).getWeatherInfoForCity(OSIJEK_CITY_NAME) | ||
verify(weatherDao, never()).updateWeatherAndReturn(fakeWeatherEntity) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.data.utils | ||
|
||
import com.example.data.database.model.WeatherEntity | ||
import com.example.data.networking.model.MainInfo | ||
import com.example.data.networking.model.WeatherInfoResponse | ||
import okhttp3.MediaType | ||
import okhttp3.ResponseBody | ||
|
||
const val OSIJEK_CITY_NAME = "Osijek" | ||
const val FAKE_FAILURE_ERROR_CODE = 400 | ||
|
||
val successWeatherInfoResponse = WeatherInfoResponse(0, arrayListOf(), MainInfo(), "") | ||
val failureResponseBody = ResponseBody.create(MediaType.parse("text"), "network error") | ||
val fakeWeatherEntity = WeatherEntity(0, arrayListOf(), MainInfo(), "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters