Hi! We’ve just added Koin to our project and we ar...
# koin
s
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?
We are doing this:
Copy code
@Before
  fun setUp() {
    declareMock<ComponentA>()

    val componentA = get<ComponentA>()
    BDDMockito.`when`(componentA.foo()).thenReturn(bar)
  }
is that correct?
j
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
        
        }
    }
}
s
yeah, looks good! Thanks!