I am trying to write a unit test cases for one of the KoinComponent class but I am getting the below error
org.koin.core.error.NoBeanDefFoundException: No definition found for class:'com.subbu.unittestsample.TestUtil2'. Check your definitions!
11-13 11:06:14.886 10707 10726 E TestRunner: at org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.kt:264)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.koin.core.scope.Scope.resolveInstance(Scope.kt:233)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.koin.core.scope.Scope.get(Scope.kt:204)
11-13 11:06:14.886 10707 10726 E TestRunner: at com.subbu.unittestsample.TestUtil$special$$inlined$inject$default$1.invoke(KoinComponent.kt:74)
11-13 11:06:14.886 10707 10726 E TestRunner: at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
11-13 11:06:14.886 10707 10726 E TestRunner: at com.subbu.unittestsample.TestUtil$Companion.getTest2(TestUtil.kt:12)
11-13 11:06:14.886 10707 10726 E TestRunner: at com.subbu.unittestsample.TestUtil$Companion.test(TestUtil.kt:21)
11-13 11:06:14.886 10707 10726 E TestRunner: at com.subbu.unittestsample.ExampleInstrumentedTest.koinTest(ExampleInstrumentedTest.kt:48)
11-13 11:06:14.886 10707 10726 E TestRunner: at java.lang.reflect.Method.invoke(Native Method)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
11-13 11:06:14.886 10707 10726 E TestRunner: at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:162)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.Suite.runChild(Suite.java:128)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.Suite.runChild(Suite.java:27)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
11-13 11:06:14.886 10707 10726 E TestRunner: at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
11-13 11:06:14.886 10707 10726 E TestRunner: at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:67)
11-13 11:06:14.886 10707 10726 E TestRunner: at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
11-13 11:06:14.886 10707 10726 E TestRunner: at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:446)
11-13 11:06:14.886 10707 10726 E TestRunner: at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2248)
11-13 11:06:14.887 10707 10726 E TestRunner: ----- end exception -----
TestUtil2.kt
class TestUtil2 : KoinComponent,ITestUtil2 {
override fun getMsg(): String {
return "TestUtil2 print"
}
}
interface ITestUtil2 {
fun getMsg():String
}
Unit test class
@Test
fun koinTest() {
startKoin {
module {
single<ITestUtil2> { TestUtil2() }
single { TestUtil() }
}
}
val test = TestUtil.test(ApplicationProvider.getApplicationContext())
println("Subbu $test")
}
TestUtil1.kt
class TestUtil: KoinComponent {
companion object:KoinComponent {
private val test2:TestUtil2 by inject()
fun test(context:Context): String? {
println("Subbu ${test2.getMsg()}")
return string
}
}
}