koin_test package brings you small but powerful tools to test your Koin application.
Getting your components
Just import the
koin_testpackage, and you will be able to unlock
KoinComponent & testing features:
inject()- lazy inject an instance
get()- retrieve an instance
Given the definitions below:
We can write the test below:
you can use the
tearDown to start/stop your Koin context:
Checking your modules
You can easily test module definitions.
Let's write our check test as follow:
- test modules with
Let's check our modules.
Mocking on the fly
Once you have import
koin_test, you can use the
declare function to declare mocks & behavior on the fly:
Starting & stopping for tests
Take attention to stop your koin instance (if you use
startKoin in your tests) between every test. Else be sure to use
koinApplication, for local koin instances or
stopKoin() to stop the current global instance.