I mean, I'm used to DaggerMock which is a wonderfu...
# koin
c
I mean, I'm used to DaggerMock which is a wonderful tool and I would like to be able to introduce mocks on UI Tests to not depend on third party services ( for my own services I mostly use WireMock )
d
I created a set of modules that return mocks and use the vararg loadKoinModules method in a setup method before I start the activity under test. It’s not magic, but it works.
c
Actually I did something easier:
Copy code
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest : AutoCloseKoinTest() {

  private val retrofit: Retrofit by inject()
  private val repository: Repository by inject()

  @Before
  fun setUp() {
    val mockModule = applicationContext {
      bean {
        Retrofit.Builder()
            .baseUrl("<http://localhost:8080/>")
            .build()
      }
    }
    startKoin(getApplication().koinModules + mockModule)
  }

  private fun getApplication() =
      InstrumentationRegistry.getTargetContext().applicationContext as MyApplication

  <@U71GL59NU>
  fun testKoin() {
    assertEquals("<http://localhost:8080/>", retrofit.baseUrl().toString())
    assertNotEquals("<http://192.168.1.40:8080/>", retrofit.baseUrl().toString())

    assertNotNull(repository)
  }
}
The change I did too is using a
MyTestApp: MyApp()
overriding where I usually startKoin to do nothing, and a custom Runner to make it use MyTestApp
a
You can use koin-test to tag your class with KoinTest
then use startKoin()
c
It's what I do, but what I didn't expect is that just by doing
startKoin(realModules + stubbedModules)
the latest will override the beans/factories from the first
a
yep
if you don”t want to override existing definitions with mocked one, what do you want to do?
c
No, no, is exactly what I want, I just didn't expect it
a
Ok 🙂