From aabce1658f5ca999b38566e739af4ed465c9ec23 Mon Sep 17 00:00:00 2001 From: Luka Date: Wed, 31 Jul 2019 11:21:46 +0200 Subject: [PATCH] Write WeatherRepository tests --- .../test/java/com/example/data/TestUtils.kt | 3 -- .../com/example/data/WeatherRepositoryTest.kt | 40 +++++++++++++++++++ .../java/com/example/data/utils/TestUtils.kt | 14 +++++++ .../com/example/domain/WeatherUseCaseTest.kt | 1 - 4 files changed, 54 insertions(+), 4 deletions(-) delete mode 100644 data/src/test/java/com/example/data/TestUtils.kt create mode 100644 data/src/test/java/com/example/data/WeatherRepositoryTest.kt create mode 100644 data/src/test/java/com/example/data/utils/TestUtils.kt diff --git a/data/src/test/java/com/example/data/TestUtils.kt b/data/src/test/java/com/example/data/TestUtils.kt deleted file mode 100644 index 56b7d51..0000000 --- a/data/src/test/java/com/example/data/TestUtils.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.example.data - -const val OSIJEK_CITY_NAME="Osijek" \ No newline at end of file diff --git a/data/src/test/java/com/example/data/WeatherRepositoryTest.kt b/data/src/test/java/com/example/data/WeatherRepositoryTest.kt new file mode 100644 index 0000000..fdef6f1 --- /dev/null +++ b/data/src/test/java/com/example/data/WeatherRepositoryTest.kt @@ -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) + } + } +} \ No newline at end of file diff --git a/data/src/test/java/com/example/data/utils/TestUtils.kt b/data/src/test/java/com/example/data/utils/TestUtils.kt new file mode 100644 index 0000000..4ed4ba5 --- /dev/null +++ b/data/src/test/java/com/example/data/utils/TestUtils.kt @@ -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(), "") diff --git a/domain/src/test/java/com/example/domain/WeatherUseCaseTest.kt b/domain/src/test/java/com/example/domain/WeatherUseCaseTest.kt index d341cdf..89c40f2 100644 --- a/domain/src/test/java/com/example/domain/WeatherUseCaseTest.kt +++ b/domain/src/test/java/com/example/domain/WeatherUseCaseTest.kt @@ -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()