0

I work on a project with Koin (I'm not familiar with Koin).

I have update Koin from 3.0.2 to 3.2.2 and the test starts to fail.

    class KoinDependenciesTest : KoinTest() {

    @get:Rule
    val koinTestRule = KoinTestRule.create {
        val mockApplication = mock<Application>()
        androidContext(mockApplication)
        // The Level.ERROR added to prevent exception java.lang.NoSuchMethodError
        printLogger(Level.ERROR)
        modules(modulesList)
        

        declareMock<PreferencesManager>()
        declareMock<ConnectivityInterceptor>()
        declareMock<SharedPreferences>()
        declareMock<SharedPrefsUtils>()
        declareMock<CrashlyticsCustomLogger>()
        declareMock<PushNotificationManager>()

        val dateTime: DateTime = mock()

        declareMock<DateManager> {
            whenever(now()).thenReturn(dateTime)
            whenever(dateTime.minusDays(any())).thenReturn(dateTime)
            whenever(dateTime.toDate()).thenReturn(mock())
        }
    }

    @get:Rule
    val mockProvider = MockProviderRule.create { clazz ->
        Mockito.mock(clazz.java)
    }

    @get:Rule
    val trampolineSchedulerRule = TrampolineSchedulerRule()

    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    @ExperimentalCoroutinesApi
    @get:Rule
    // use to call runBlockingTest {} for components with coroutine functions
    val coroutinesDispatcherRule: CoroutinesMainDispatcherRule = CoroutinesMainDispatcherRule()

    @ObsoleteCoroutinesApi
    @OptIn(ExperimentalCoroutinesApi::class)
    @Test
    fun `Checking dependencies loading`() {
        checkModules {
            modules(modulesList)
        }
    }    
}
  1. I get an error

    A Koin Application has already been started

I fixed it by change KoinTest to AutoCloseKoinTest

  1. Now i can't pass the Checking dependencies loading. Error

    Could not create instance for....

    ...

    Caused by: org.koin.core.error.NoBeanDefFoundException

I have much more classes then defined in declareMock and a few additional functions to test initialisations of different classes.

How I can test modules loading?

Lavan
  • 157
  • 9

0 Answers0