@@ -2,20 +2,17 @@ package domain.use_cases
22
33import GetCurrentWeatherUseCase
44import WeatherRepository
5+ import com.google.common.truth.Truth
6+ import com.google.common.truth.Truth.assertThat
57import domain.models.CurrentWeather
68import io.mockk.coEvery
79import io.mockk.coVerify
810import io.mockk.mockk
9- import kotlinx.coroutines.ExperimentalCoroutinesApi
1011import kotlinx.coroutines.test.runTest
11- import org.junit.Assert.assertEquals
12- import org.junit.Assert.fail
1312import org.junit.jupiter.api.BeforeEach
1413import org.junit.jupiter.api.Test
15- import com.google.common.truth.Truth.assertThat
1614import org.junit.jupiter.api.assertThrows
1715
18- @OptIn(ExperimentalCoroutinesApi ::class )
1916class GetCurrentWeatherUseCaseTest {
2017
2118 private lateinit var weatherRepository: WeatherRepository
@@ -24,15 +21,12 @@ class GetCurrentWeatherUseCaseTest {
2421 private val latitude = 55.7558
2522 private val longitude = 37.6173
2623 private val expectedWeather = CurrentWeather (
27- time = " 2025-05-06T15:00" ,
28- interval = 100.0 ,
2924 temperature2m = 12.5 ,
3025 relativeHumidity2m = 67.0 ,
3126 apparentTemperature = 11.2 ,
3227 isDay = 1 ,
3328 rain = 0.1 ,
3429 snowfall = 0.0 ,
35- weatherCode = 3.0 ,
3630 cloudCover = 65.0 ,
3731 windSpeed10m = 6.8 ,
3832 )
@@ -43,6 +37,19 @@ class GetCurrentWeatherUseCaseTest {
4337 getCurrentWeatherUseCase = GetCurrentWeatherUseCase (weatherRepository)
4438 }
4539
40+ @Test
41+ fun `should get the current weather from the WeatherRepository when getting the current weather` () =
42+ runTest {
43+ // Given
44+ coEvery { weatherRepository.getCurrentWeather(latitude, longitude) } returns expectedWeather
45+
46+ // When
47+ val result = getCurrentWeatherUseCase.getCurrentWeather(latitude, longitude)
48+
49+ // Then
50+ coVerify(exactly = 1 ) { weatherRepository.getCurrentWeather(latitude, longitude) }
51+ }
52+
4653 @Test
4754 fun `should return current weather for given coordinates when getting the current weather` () = runTest {
4855 // Given
@@ -52,13 +59,13 @@ class GetCurrentWeatherUseCaseTest {
5259 val result = getCurrentWeatherUseCase.getCurrentWeather(latitude, longitude)
5360
5461 // Then
55- coVerify(exactly = 1 ) { weatherRepository.getCurrentWeather(latitude, longitude) }
56- assertEquals(expectedWeather, result)
62+ Truth .assertThat(expectedWeather).isEqualTo(result)
5763 }
5864
5965 @Test
6066
61- fun `should throw exception when repository fails` () = runTest {
67+ fun `fun getCurrentWeather() should throw exception when repository fails` () = runTest {
68+
6269 // Given
6370 val exceptionMessage = " API error"
6471 coEvery { weatherRepository.getCurrentWeather(latitude, longitude) } throws RuntimeException (exceptionMessage)
@@ -73,29 +80,34 @@ class GetCurrentWeatherUseCaseTest {
7380 }
7481
7582 @Test
76- fun `should return weather with valid temperature value` () = runTest {
77- // Given
78- val modifiedWeather = expectedWeather.copy(temperature2m = 20.8 )
79- coEvery { weatherRepository.getCurrentWeather(latitude, longitude) } returns modifiedWeather
80-
81- // When
82- val result = getCurrentWeatherUseCase.getCurrentWeather(latitude, longitude)
83+ fun `getCurrentWeather() should return expected weather data when valid latitude and longitude are provided()` () =
84+ runTest {
85+ // Given
86+ val testLat = 33.3
87+ val testLon = 44.4
88+ val expected = expectedWeather.copy(temperature2m = 25.0 )
89+ coEvery { weatherRepository.getCurrentWeather(testLat, testLon) } returns expected
90+
91+ // When
92+ val result = getCurrentWeatherUseCase.getCurrentWeather(testLat, testLon)
93+
94+ // Then
95+ coVerify { weatherRepository.getCurrentWeather(testLat, testLon) }
96+ assertThat(result).isEqualTo(expected)
97+ }
8398
84- // Then
85- assertThat(result.temperature2m).isEqualTo(20.8 )
86- }
8799
88100 @Test
89- fun `should call repository with zero coordinates` () = runTest {
90- // Given
91- val zeroLat = 0.0
92- val zeroLon = 0.0
93- coEvery { weatherRepository.getCurrentWeather(zeroLat, zeroLon) } returns expectedWeather
101+ fun `getCurrentWeather() should call repository with zero coordinates when latitude and longitude are both zeros ` () =
102+ runTest {
103+ val zeroLat = 0.0
104+ val zeroLon = 0.0
105+ coEvery { weatherRepository.getCurrentWeather(zeroLat, zeroLon) } returns expectedWeather
94106
95- // When
96- val result = getCurrentWeatherUseCase.getCurrentWeather(zeroLat, zeroLon)
107+ // When
108+ getCurrentWeatherUseCase.getCurrentWeather(zeroLat, zeroLon)
97109
98- // Then
99- coVerify { weatherRepository.getCurrentWeather(zeroLat, zeroLon) }
100- }
110+ // Then
111+ coVerify { weatherRepository.getCurrentWeather(zeroLat, zeroLon) }
112+ }
101113}
0 commit comments