Skip to content

Commit

Permalink
fix InsertKoinIO#1213 Mock instance check
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Dec 17, 2021
1 parent aa7f660 commit b462f16
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.koin.test.android

import android.content.Context
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.check.checkKoinModules
import org.koin.test.check.checkModules
import org.koin.test.mock.MockProviderRule
import org.mockito.Mockito
import kotlin.reflect.KClass

class PrefsManager(val context: Context)

// KoinModulesTest.kt
class KoinModulesTest : KoinTest {

@get:Rule
val rule: TestRule = InstantTaskExecutorRule()

// KoinAppModule.kt
val prefsModule = module {
single { PrefsManager(context = get()) } // package.PrefsManager constructor(context: Context)
}

@get:Rule
val mockProvider = MockProviderRule.create { kClass: KClass<*> ->
// invokes properly with android.content.Context
Mockito.mock(kClass.java)
}

@Test
fun checkAllModules() {
checkKoinModules(listOf(prefsModule)){
withInstance<Context>()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.named
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.test.check.checkKoinModules
import org.koin.test.check.checkModules
import org.koin.test.mock.MockProviderRule
import org.mockito.Mockito
Expand Down Expand Up @@ -39,6 +40,21 @@ class CheckModulesTest {
}
}

@Test
fun `check a module - all dsl - checkKoinModules`() {
val modules = module {
single { p -> Simple.ComponentB(p.get()) }
single(named("param")) { p -> Simple.MyString(p.get()) }
single { Simple.MyString(getProperty("aValue")) }
}

checkKoinModules(listOf(modules)) {
withInstance<Simple.ComponentA>()
withParameter<String> { "a_parameter" }
withProperty("aValue", "string_value")
}
}

@Test
fun `check a module - named dsl value`() {
val modules = module {
Expand Down Expand Up @@ -240,6 +256,15 @@ class CheckModulesTest {
}.checkModules()
}

@Test
fun `check a module with link - checkKoinModules`() {
val m = module {
single { Simple.ComponentA() }
single { Simple.ComponentB(get()) }
}
checkKoinModules(listOf(m))
}

@Test
fun `check a broken module`() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,29 @@ fun checkModules(level: Level = Level.INFO, parameters: CheckParameters? = null,

/**
* @see checkModules
*
* Deprecated
*/
@Deprecated("use instead checkKoinModules(modules : List<Module>, appDeclaration: KoinAppDeclaration = {}, parameters: CheckParameters? = null)")
fun checkKoinModules(level: Level = Level.INFO, parameters: CheckParameters? = null, appDeclaration: KoinAppDeclaration) {
startKoin(appDeclaration)
.logger(KoinPlatformTools.defaultLogger(level))
.checkModules(parameters)
stopKoin()
}

/**
* Check given modules directly
*
* @param modules - list of modules
* @param appDeclaration - Koin app config if needed
* @param parameters - Check parameters DSL
*/
fun checkKoinModules(modules : List<Module>, appDeclaration: KoinAppDeclaration = {}, parameters: CheckParameters? = null) {
startKoin(appDeclaration)
.modules(modules)
.checkModules(parameters)
stopKoin()
}

/**
Expand Down
8 changes: 5 additions & 3 deletions examples/androidx-samples/src/test/java/CheckModulesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.dsl.koinApplication
import org.koin.sample.androidx.di.allTestModules
import org.koin.test.check.checkModules
Expand All @@ -27,11 +28,12 @@ class CheckModulesTest {
@Test
fun `test DI modules`() {
startKoin {
printLogger(Level.DEBUG)
modules(allTestModules)
checkModules {
withInstance<Context>()
withInstance<Activity>()
withInstance<Application>()
// withInstance<Context>()
// withInstance<Activity>()
// withInstance<Application>()
withInstance<SavedStateHandle>()
withInstance<WorkerParameters>()
}
Expand Down

0 comments on commit b462f16

Please sign in to comment.