Hi! We’ve just added Koin to our project and we are facing the test part. We’d like to substitute a provided dependency with a mock in an android test. How should we do it?
smassive
11/28/2018, 11:03 AM
We are doing this:
Copy code
@Before
fun setUp() {
declareMock<ComponentA>()
val componentA = get<ComponentA>()
BDDMockito.`when`(componentA.foo()).thenReturn(bar)
}
smassive
11/28/2018, 11:03 AM
is that correct?
j
Johannes Waltsgott
11/28/2018, 11:14 AM
We’re doing it like this (Koin 2.0.0-alpha2)
Copy code
@LargeTest
@RunWith(AndroidJUnit4::class)
class MockedTest : KoinTest {
// will be mocked
val contactFormRepo: ContactFormRepository by inject()
@Rule
@JvmField
// As interaction with mock starts in activity's
// onCreate we can't launch it before mock configuration
val rule = object :
ActivityTestRule<ContactFormActivity>(ContactFormActivity::class.java, false, false) {}
@Before
fun setUp() {
declareMock<ContactFormRepository>()
}
@After
fun tearDown() {
rule.finishActivity()
}
@Test
fun shouldShowError() {
whenever(contactFormRepo.submitForm(...)) .thenReturn(Completable.error(Throwable()))
rule.launchActivity(null)
// test goes here
}
}
}