Describe the bug
After the update to 1.0.2, tests are not having Koin started.
Downgrading to 1.0.1 makes the tests working as expected.
To Reproduce
// Imports you want to see
import com.doctoror.particleswallpaper.framework.di.KoinContextProvider
import org.koin.standalone.StandAloneContext
import org.koin.standalone.inject
import org.koin.test.KoinTest
import org.koin.test.declareMock
@RunWith(RobolectricTestRunner::class)
class ParticleScalePreferenceTest : KoinTest {
private val presenter: ParticleScalePreferencePresenter by inject()
private val underTest =
ParticleScalePreference(ApplicationProvider.getApplicationContext<Context>())
@Before
fun setup() {
KoinContextProvider.getKoinContext(
ApplicationProvider.getApplicationContext<Context>()) // this will call startKoin if not already started
declareMock<ParticleScalePreferencePresenter>()
}
@Test
fun deliversOnStartToPresenter() {
// When
underTest.onStart()
// Then
verify(presenter).onStart()
}
Expected behavior
Injection working in tests
Actual behavior
java.lang.IllegalStateException: StandAloneContext Koin instance is null
at org.koin.standalone.StandAloneContext.getKoin(StandAloneContext.kt:68)
at com.doctoror.particleswallpaper.framework.di.KoinContextProvider.getKoinContext(KoinContextProvider.kt:39)
Where
/**
* To avoid crashes when Koin is not getting initialized or getting initialized multiple times, do
* not rely on Application to initialize it. Is a solution to:
*
* https://github.com/InsertKoinIO/koin/issues/286
*/
object KoinContextProvider {
private var koinStarted = false
@Synchronized
fun getKoinContext(context: Context): KoinContext {
if (!koinStarted) {
koinStarted = true
KoinStarter().startKoin(context.applicationContext)
}
return StandAloneContext.getKoin().koinContext
}
}
Koin project used and used version (please complete the following information):
koin-core version 1.0.2`
Additional moduleDefinition
startKoin is called lazily in my app when first inject happens:
Injecting into implementation as follows:
// Imports you want to see
import com.doctoror.particleswallpaper.framework.di.inject
private val presenter: ParticleScalePreferencePresenter by inject(
context = context,
parameters = { parametersOf(this as SeekBarPreferenceView) }
)
Where inject and get are
inline fun <reified T : Any> inject(
context: Context,
name: String = "",
scope: Scope? = null,
noinline parameters: ParameterDefinition = emptyParameterDefinition()
) = lazy { get<T>(context, name, scope, parameters) }
inline fun <reified T : Any> get(
context: Context,
name: String = "",
scope: Scope? = null,
noinline parameters: ParameterDefinition = emptyParameterDefinition()
): T = KoinContextProvider.getKoinContext(context).get(name, scope, parameters)
did u manage to fix this?
@jonneymendoza no, I am still using 1.0.1 and cannot upgrade.
I'm seeing this issue too in the XML interface editor
Is this fixed 2.0?
@scottandrew I don't have any reproducing use case for that. Can you provide one?
You can also check in 2.0-beta-1 if it's ok.
I don't have a sample I cab share. But its as simple as having a view subclass defined sort of the like the following:
class FooView @JVMOverloads constructor(context: Context, attrs: AttributeSet = null, defStyleAttr: Int = 0): View(context, attires, defStyleAttr)) {
// you can have the get here or in a function that is needed by android studio's preview.
val somBean: MyBean = get()
... rest of code here
}
The use that fragment in the layout XML
... bunch of attributes
So basically have a view use the inject on a custom view that you want rendered in Studio's designer interface and it will not render. I haven't tried 2.0 yet as we are trying to ship.
@arnaudgiuliani to reproduce, update Koin to 1.0.2 in this project and run the tests.
https://github.com/Doctoror/ParticleConstellationsLiveWallpaper
While 1.0.2 fails like above, 1.0.1 throws java.lang.NoClassDefFoundError: org/koin/test/KoinTest$DefaultImpls. I am lost