-
-
Notifications
You must be signed in to change notification settings - Fork 712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding "includeAndroidResources" flag makes koin starts twice in roboelectric tests #254
Comments
I had a very similar problem (BeanOverrideException in Robolectric test) but found out that it wasn't a Koin's fault. The problem was in the way I registered the modules to be included in startKoin() method. I had all Koin module definitions in a static list variable. Here's an example of what it looked like when I had a problem: @RunWith(AndroidJUnit4::class)
class SomeTest : AutoCloseKoinTest() {
@Before
fun before() {
startKoin(appModules("..."))
}
} Here's how I solved a problem: val appModules by lazy {
appModules("...")
}
@RunWith(AndroidJUnit4::class)
class SomeTest : AutoCloseKoinTest() {
@Before
fun before() {
startKoin(appModules)
}
} The classes/functions used in the example: private fun appModules(vararg whitelistPackages: String): List<Module> {
val classGraph = ClassGraph().apply {
enableAllInfo()
whitelistPackages(*whitelistPackages)
}
val scanResult = classGraph.scan()
val appModules = scanResult.getSubclasses(AppModule::class.qualifiedName)
appModules.forEach {
val kClass = Class.forName(it.name).kotlin
val instance = kClass.constructors.first().call()
(instance as AppModule).onCreate()
}
return AppModule.registeredModules
}
abstract class AppModule(
private val definition: ModuleDefinition.() -> Unit
) : ContentProvider() {
companion object {
private val modules = mutableListOf<Module>()
val registeredModules: List<Module>
get() = modules
}
var module: Module
get
private set
init {
module = module(Path.ROOT, false, false, definition)
}
override fun onCreate(): Boolean {
modules += module
return true
}
} Hope this will help someone. |
I also have this problem but the solution above is not feasible, since I encounter the exception when creating the Application object:
In my Application class, I start Koin in the |
Hello guys. To Reproduce Expected behavior Koin project used and used version (please complete the following information): |
With Koin 2.0.0-alpha-6 this issue seems to be fixed. I have successfully used this version of Koin with Robolectric 4.1, with the I did have a moment where I could not override some dependencies in my test but it turns out I was using it wrong. For sake of completion, this is how I used it
Notice the use of |
Having issues w/ Koin 2.0.0-beta-1 trying to swap out Kodein as well...complains Koin was started twice. Why can't it just ignore the second startKoin() request and make it a warning instead? |
I think it's because RoboElectric tests run in parallel...fixed it by making my app's Koin init check whether it's been initialized like so:
Everything passes with this change :-) |
linked to startKoin or loadModules with a verification to see if it's already started |
to follow on next 2.1.x. reopen if needed |
Description
Hello. It seems that using "includeAndroidResources" flag makes koin starts twice in tests, even if it's not used there. Here is the link to repo with test behavior
https://github.com/esdudnik/mapwall
branch "koin-error".
I have 2 unit tests that worked perfectly. One is extended from AutoCloseKoinTest (MapCacheStrategyTest), another one has no parent(SharedPreferencesStorageTest).
After adding
testOptions {
unitTests {
includeAndroidResources = true
}
}
(recommendation from Robolectric), MapCacheStrategyTest run crashed with error "Singleton instance already exists"(seems that App.kt starts twice) and SharedPreferencesStorageTest crashed with "BeanOverrideException - Try to override definition with Single".
I asked a question in slack channel, and show example of tests and project, and guys said that it seems that koin starts twice in robolectic tests, and recommend to create a repo with that reproduces such problem.
To Reproduce
Run gradlew build or separate tests to see error.
Expected behavior
Actually after adding adding "includeAndroidResources" I expect everything to work as before.
Koin project used and used version (please complete the following information):
'koin-android-viewmodel version 1.0.0'
The text was updated successfully, but these errors were encountered: