Checking your modules or application graph
note
Koin allows you to verify your configuration modules, avoiding to discover dependency injection issues at runtime.
To verify your modules, you just need to the checkModules()
function within a simple JUnit test. This will launch your modules and try to run each possible definition for you.
also possible to use checkKoinModules
:
CheckModule DSL
For any definition that is using injected parameters, properties or dynamic instances, the checkModules
DSL allow to specify how to work with the following case:
withInstance(value)
- will addvalue
instance to Koin graph (can be used in dependency or parameter)withInstance<MyType>()
- will add a mocked instance ofMyType
. Use MockProviderRule. (can be used in dependency or parameter)withParameter<Type>(qualifier){ qualifier -> value }
- will addvalue
instance to be injected as parameterwithParameter<Type>(qualifier){ qualifier -> parametersOf(...) }
- will addvalue
instance to be injected as parameterwithProperty(key,value)
- add property to Koin
Allow mocking with a Junit rule
To use mocks with checkModules
, you need to provide a MockProviderRule
Verifying modules with dynamic behavior (3.1.3+)
To verify a dynamic behavior like following, let's use the CheckKoinModules DSL to provide the missing instance data to our test:
You can verify it with the following:
This way, FactoryPresenter
definition will be injected with "_my_id_value"
define above.
You can also use mocked instances, to fill up your graph. You can notice that we need a MockProviderRule
declaration to allow Koin mock any injected definition
Checking Modules for Android (3.1.3)
Here below is how you can test your graph for a typical Android app:
also possible to use checkKoinModules
:
Providing Default Values (3.1.4)
If you need, you can set a default value for all type in the checked modules. For example, We can override all injected string values:
Let's use the withInstance()
function in checkModules
block, to define a default value for all definitions:
All injected definition that are using a injected String
parameter, will receive "_ID_"
:
Providing ParametersOf values (3.1.4)
You can define default value to be injected for one specific definition, with withParameter
or withParameters
functions:
Providing Scope Links
You can link scopes by using withScopeLink
function incheckModules
block to inject instances from another scope's definitions: