-
-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1977 from InsertKoinIO/verify_parameter_injection
Verify API - allow declare parameter injection type to be verified
- Loading branch information
Showing
10 changed files
with
270 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
title: Verifying your Koin configuration | ||
--- | ||
|
||
Koin allows you to verify your configuration modules, avoiding discovering dependency injection issues at runtime. | ||
|
||
## Koin Configuration check with Verify() - JVM Only [3.3] | ||
|
||
Use the verify() extension function on a Koin Module. That's it! Under the hood, This will verify all constructor classes and crosscheck with the Koin configuration to know if there is a component declared for this dependency. In case of failure, the function will throw a MissingKoinDefinitionException. | ||
|
||
```kotlin | ||
val niaAppModule = module { | ||
includes( | ||
jankStatsKoinModule, | ||
dataKoinModule, | ||
syncWorkerKoinModule, | ||
topicKoinModule, | ||
authorKoinModule, | ||
interestsKoinModule, | ||
settingsKoinModule, | ||
bookMarksKoinModule, | ||
forYouKoinModule | ||
) | ||
viewModelOf(::MainActivityViewModel) | ||
} | ||
``` | ||
|
||
```kotlin | ||
class NiaAppModuleCheck { | ||
|
||
@Test | ||
fun checkKoinModule() { | ||
|
||
// Verify Koin configuration | ||
niaAppModule.verify() | ||
} | ||
} | ||
``` | ||
|
||
|
||
Launch the JUnit test and you're done! ✅ | ||
|
||
|
||
As you may see, we use the extra Types parameter to list types used in the Koin configuration but not declared directly. This is the case for SavedStateHandle and WorkerParameters types, that are used as injected parameters. The Context is declared by androidContext() function at start. | ||
|
||
The verify() API is ultra light to run and doesn't require any kind of mock/stub to run on your configuration. | ||
|
||
## Verifying with Injected Parameters - JVM Only [4.0] | ||
|
||
When you have a configuration that implies injected obects with `parametersOf`, the verification will fail because there is no definition of the parameter's type in your configuration. | ||
However you can define a parameter type, to be injected with given definition `definition<Type>(Class1::class, Class2::class ...)`. | ||
|
||
Here is how it goes: | ||
|
||
```kotlin | ||
class ModuleCheck { | ||
|
||
// given a definition with an injected definition | ||
val module = module { | ||
single { (a: Simple.ComponentA) -> Simple.ComponentB(a) } | ||
} | ||
|
||
@Test | ||
fun checkKoinModule() { | ||
|
||
// Verify and declare Injected Parameters | ||
module.verify( | ||
injections = injectedParameters( | ||
definition<Simple.ComponentB>(Simple.ComponentA::class) | ||
) | ||
) | ||
} | ||
} | ||
``` | ||
|
||
## Type White-Listing | ||
|
||
We can add types as "white-listed". This means that this type is considered as present in the system for any definition. Here is how it goes: | ||
|
||
```kotlin | ||
class NiaAppModuleCheck { | ||
|
||
@Test | ||
fun checkKoinModule() { | ||
|
||
// Verify Koin configuration | ||
niaAppModule.verify( | ||
// List types used in definitions but not declared directly (like parameters injection) | ||
extraTypes = listOf(MyType::class ...) | ||
) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
projects/core/koin-test/src/jvmMain/kotlin/org/koin/test/verify/ParameterTypeInjection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.koin.test.verify | ||
|
||
import org.koin.core.annotation.KoinExperimentalAPI | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* ParameterTypeInjection is a proposal to allow describe types that are dynamic, and needs injection parameters (use of parametersOf) | ||
* | ||
* @author Arnaud Giuliani | ||
*/ | ||
|
||
/** | ||
* Define Parameter Injection Types in order to help verify definition | ||
*/ | ||
@KoinExperimentalAPI | ||
data class ParameterTypeInjection(val targetType : KClass<*>, val injectedTypes : List<KClass<*>>) | ||
|
||
/** | ||
* Define injection for a definition Type | ||
* @param T - definition type | ||
* @param injectedParameterTypes - Types that need to be injected later with parametersOf | ||
*/ | ||
@KoinExperimentalAPI | ||
inline fun <reified T> definition(vararg injectedParameterTypes : KClass<*>): ParameterTypeInjection{ | ||
return ParameterTypeInjection(T::class, injectedParameterTypes.toList()) | ||
} | ||
|
||
/** | ||
* Define injection for a definition Type | ||
* @param T - definition type | ||
* @param injectedParameterTypes - Types that need to be injected later with parametersOf | ||
*/ | ||
@KoinExperimentalAPI | ||
inline fun <reified T> definition(injectedParameterTypes : List<KClass<*>>): ParameterTypeInjection{ | ||
return ParameterTypeInjection(T::class, injectedParameterTypes) | ||
} | ||
|
||
/** | ||
* Declare list of ParameterTypeInjection - in order to help define parmater injection types to allow in verify | ||
* @param injectionType - list of ParameterTypeInjection | ||
*/ | ||
@KoinExperimentalAPI | ||
fun injectedParameters(vararg injectionType : ParameterTypeInjection) : List<ParameterTypeInjection>{ | ||
return injectionType.toList() | ||
} |
Oops, something went wrong.