Solution 1 :
This might not be a complete answer because there is so much in your question. Start by trying to use a CoroutineTestRule:
@ExperimentalCoroutinesApi
class CoroutineTestRule(
private val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {
override fun starting(description: Description?) {
Dispatchers.setMain(testDispatcher)
}
override fun finished(description: Description?) {
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
}
Your test will be something like:
class PaymentViewModelTest : KoinTest {
private val paymentViewModel : PaymentViewModel by inject()
@get:Rule
val coroutineTestRule = CoroutineTestRule()
@Before
fun setup(){
startKoin {
androidContext(mock(Application::class.java))
modules(
modules.repositoryModule,
modules.businessModule,
modules.utilsModule
)
}
declareMock<AnalyticsHelper>()
declareMock<Printer>()
}
@After
fun after(){
stopKoin()
}
// Other methods are the same.
}
You can use an AutoCloseKoinTest to remove that after() method.
You say that the test is passing when you run it isolated, so maybe this is enough. But there is more to dig into if this doesn’t work. For example, I find it strange that you use runBlockingTest inside a mock and the assert is outside that block. Usually I would use MockK to mock suspending functions and test and assert any of them inside a runBlockingTest.
Problem :
I am testing a suspended method from my ViewModel
that triggers LiveData
to emit an object when coroutine is completed. When
I run each of those tests individually they pass, when I run them together always the first test fails. Surprisingly, when I run them in debug and I put break points at assertValue
to check what the vaule is, both of the test pass. My guess is that the problem is with the state of LiveData
or the whole PaymentViewModel
. What am I doing wrong?
class PaymentViewModelTest : KoinTest {
private val paymentViewModel : PaymentViewModel by inject()
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
private val mainThreadSurrogate = newSingleThreadContext("UI thread")
@Before
fun setup(){
Dispatchers.setMain(mainThreadSurrogate)
val modules = KoinModule()
startKoin {
androidContext(mock(Application::class.java))
modules(listOf(
modules.repositoryModule,
modules.businessModule,
modules.utilsModule)
)
}
declareMock<AnalyticsHelper>()
declareMock<Printer>()
}
@After
fun after(){
stopKoin()
Dispatchers.resetMain()
}
@Test
fun successfully_initializes_payment_flow() {
declareMock<PaymentRepository> {
runBlockingTest {
given(initPayment())
.willAnswer { InitPaymentResponse(0, PaymentStatus.INITIALIZED, 0) }
}
}
paymentViewModel.initPayment(BigDecimal(0))
paymentViewModel.paymentStatus.test()
.awaitValue()
.assertValue { value -> value.getContentIfNotHandled()?.data == PaymentStatus.INITIALIZED }
}
@Test
fun fails_to_initialize_payment_flow() {
declareMock<PaymentRepository> {
runBlockingTest {
given(initPayment())
.willThrow(MockitoKotlinException("", ConnectException()))
}
}
paymentViewModel.initPayment(BigDecimal(0))
paymentViewModel.paymentStatus.test()
.awaitValue()
.assertValue { value -> value.getContentIfNotHandled()?.status == ApiResponseStatus.ERROR}
}
}
Here is the method that I am testing:
fun initPayment(price: BigDecimal) {
paymentStatus.postValue(Event(ApiResponse.loading()))
viewModelScope.launch {
runCatching {
repository.initPayment()
}.onSuccess {
paymentSession = PaymentSession(it.paymentId)
paymentSession.price = price
postPaymentStatus(it.status)
}.onFailure {
postApiError(it)
}
}
}
private fun postPaymentStatus(status: PaymentStatus) =
paymentStatus.postValue(Event(ApiResponse.success(status)))