Hi there! I want to write some JUnit tests for a c...
# koin
h
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
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()
This repo has a example: https://github.com/InsertKoinIO/getting-started-koin-core.git 😄 Hope it helps
h
Thank you for the link looks like the HelloMockTest is doing exactly the same thing I want to achieve. Will help for sure 🙂
g
😄 👍
h
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
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