diff --git a/app/src/test/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcherTest.kt b/app/src/test/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcherTest.kt index 14a7a31..e4a7939 100644 --- a/app/src/test/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcherTest.kt +++ b/app/src/test/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcherTest.kt @@ -18,14 +18,39 @@ class BreezyWeatherFetcherTest { private val prefsName = "lwidget_breezy_weather_data" private val keyWeatherJson = "weather_json" + private lateinit var mockEditor: SharedPreferences.Editor + @Before fun setup() { - mockPrefs = mock() + mockEditor = mock { + on { putString(org.mockito.kotlin.any(), org.mockito.kotlin.any()) } doReturn it + } + mockPrefs = mock { + on { edit() } doReturn mockEditor + } mockContext = mock { on { getSharedPreferences(prefsName, Context.MODE_PRIVATE) } doReturn mockPrefs } } + @Test + fun `saveLatestWeatherData saves json string to preferences`() { + val validJson = """{"temp": 20}""" + + BreezyWeatherFetcher.saveLatestWeatherData(mockContext, validJson) + + org.mockito.kotlin.verify(mockEditor).putString(keyWeatherJson, validJson) + org.mockito.kotlin.verify(mockEditor).apply() + } + + @Test + fun `saveLatestWeatherData with empty string saves empty string`() { + BreezyWeatherFetcher.saveLatestWeatherData(mockContext, "") + + org.mockito.kotlin.verify(mockEditor).putString(keyWeatherJson, "") + org.mockito.kotlin.verify(mockEditor).apply() + } + @Test fun `fetchLocalWeather with valid json returns parsed data`() { val validJson = """ diff --git a/pr_description.txt b/pr_description.txt new file mode 100644 index 0000000..494a54f --- /dev/null +++ b/pr_description.txt @@ -0,0 +1,5 @@ +🎯 **What:** The testing gap addressed: Added tests for the `saveLatestWeatherData` function in `BreezyWeatherFetcher.kt` which was missing from the test suite. + +📊 **Coverage:** What scenarios are now tested: The test covers the interaction with `SharedPreferences.Editor`, ensuring both `putString` and `apply` are called correctly. It tests the happy path with valid JSON as well as an edge case with an empty string. + +✨ **Result:** The improvement in test coverage: Increased test coverage for `BreezyWeatherFetcher`, guaranteeing that weather data caching works as expected.