Hello guys. I have a small project using koin. I s...
# koin
e
Hello guys. I have a small project using koin. I stared to write unit test for some classes and face a strange issue. From the beginning everything was fine, but after I added next code in build.gradle file(took this from Robolectric official site)
Copy code
testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
all of my tests crashed with strange errors. From the beginning I received
Copy code
org.koin.error.AlreadyStartedException: Koin is already started. Run startKoin only once or use loadKoinModules
and when I added
testImplementation 'org.koin:koin-test:1.0.0'
I received error
Copy code
java.lang.NoSuchMethodError: org.koin.standalone.StandAloneContext.startKoin$default(Lorg/koin/standalone/StandAloneContext;Ljava/util/List;ZLjava/util/Map;ILjava/lang/Object;)Lorg/koin/Koin;
I extend my test classes from AutoCloseKoinTest() but errors stay the same. Do anybody faced this problem? Or probably somebody know how to fix it. I will appreciate any help.
t
main problem - Koin starts twice. I suppose Robolectic calls your application class where koin starts. Possible solution:
@Config(application = TestApplication.class)
or don't start koin in test itself
e
Hi Yahor. Thanks for your response. There no any koin classes or logic inside test. Code for test class looks next
Copy code
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
class CacheTest {

    @Before
    fun setup() {
...
       }

    @Test
    fun getDefaultCachePath {
        whenever(...)
        ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED)

        assertEquals(...)
    }
}
So there is no actual any koin logic inside test. But using
Copy code
testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
probably cause test start my app class
t
Robolectric by default starts production application class, where usually koin
startKoin()
is called, so try to provide your own test application class
hm, sorry, missed your comment that there is no any logic in test
then it is a little strange that koin starts twice in robolectic tests. Please open an issue with link to the repo that reproduces such problem.