Skip to content

Commit

Permalink
Merge branch 'feature/tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaKordic committed Jul 31, 2019
2 parents 54f1052 + aabce16 commit 3c86346
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
3 changes: 0 additions & 3 deletions data/src/test/java/com/example/data/TestUtils.kt

This file was deleted.

40 changes: 40 additions & 0 deletions data/src/test/java/com/example/data/WeatherRepositoryTest.kt
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)
}
}
}
14 changes: 14 additions & 0 deletions data/src/test/java/com/example/data/utils/TestUtils.kt
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(), "")
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.junit.MockitoJUnitRunner

@RunWith(MockitoJUnitRunner::class)
class WeatherUseCaseTest {

private val weatherRepository: WeatherRepository = mock()
Expand Down

0 comments on commit 3c86346

Please sign in to comment.