Ashinch / ReadYou

An Android RSS reader presented in Material You style.
GNU General Public License v3.0
5.11k stars 202 forks source link

OPMLDataSourceTest errors in setUp about mockContext.currentAccountId #770

Closed hufman closed 4 months ago

hufman commented 5 months ago

1. Environment

2. Describe the bug

The recently added OPMLDataSourceTest test case does not succeed in my environment. I think my environment is running the underlying extension function as part of mocking `when`(mockContext.currentAccountId).thenReturn(1):

applicationContext must not be null
java.lang.NullPointerException: applicationContext must not be null
    at androidx.datastore.preferences.PreferenceDataStoreSingletonDelegate.getValue(PreferenceDataStoreDelegate.android.kt:106)
    at androidx.datastore.preferences.PreferenceDataStoreSingletonDelegate.getValue(PreferenceDataStoreDelegate.android.kt:80)
    at me.ash.reader.ui.ext.DataStoreExtKt.getDataStore(DataStoreExt.kt:24)
    at me.ash.reader.ui.ext.DataStoreExtKt.getCurrentAccountId(DataStoreExt.kt:31)
    at me.ash.reader.infrastructure.rss.OPMLDataSourceTest.setUp(OPMLDataSourceTest.kt:51)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at jdk.proxy1/jdk.proxy1.$Proxy2.processTestClass(Unknown Source)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

I tried a few alternative ways to set up the mock, but I can't get this test to work. How do you get the unit tests to pass in your environment?

hufman commented 5 months ago

I've prepared a commit that runs the unit tests in Github Actions, to demonstrate the issue and for a future PR if you'd like.

aualbert commented 5 months ago

This does not work on my side either.

hufman commented 5 months ago

I drafted a workaround that provides an explicit PreferencesReader injectable as a concrete class to mock, instead of trying to mock extension methods, and it works for me. I'm not sure if this is the best place for this class within the project architecture, or if it should be changed to just receive a Preferences collection or the overall Settings data class or specific Preference keys. Let me know what you think, and I could update the other Context.datastore usages to match.

Ashinch commented 4 months ago

Done ✅