Hi there!
I want to write some JUnit tests for a class which injects some dependencies via Koin. I want to mock these dependencies and deliver them via Koin. In my test class I did the following:
* I implement the
KoinTest
interface
* create the mock
* create a Koin module with the mock inside
* at setup I start Koin
The output is as follows:
Copy code
[DEBUG] [Koin] instances started in 0.024431 ms
[DEBUG] [Koin] +- get 'xyz.application.LocalizationHelper'
org.koin.core.error.NoBeanDefFoundException: No definition found for 'xyz.application.LocalizationHelper' has been found. Check your module definitions.
Am I doing something wrong or isn't it possible to inject mocks that way? If not, what would be the best practice to achive what I want via Koin?
Thanks a lot!
g
gvetri
09/04/2019, 10:34 AM
I had this error yesterday. In my module defitnition i used
single { MockWalletUseCase() }
to create my mock dependency. Then in my test i used
private val mockWalletUseCase: MockWalletUseCase by inject()
Thank you for the link looks like the HelloMockTest is doing exactly the same thing I want to achieve. Will help for sure π
g
gvetri
09/04/2019, 1:51 PM
π π
h
Henning B
09/05/2019, 1:43 PM
just had some more time to inspect the github. They don't create a module for testing but use the actual module which is also used in the actual app.
than they use
declareMock
to overwrite the implemantations with mocks.
Did I see that correctly?
g
gvetri
09/06/2019, 6:54 AM
I think itβs ok. If you want to mock it, and verify it with mockito (HelloMocktest.kt). If you want to assert the return value from a method you can use what is in the HelloAppTest.kt