#StackBounty: #android #unit-testing #kotlin-coroutines UnitTest coroutines Kotlin usecase MVP

Bounty: 100

I am trying to mock a response from my usecases, this usecase works with coroutines.

fun getData() {
    getProductsUseCase.execute(this::onSuccessApi, this::onErrorApi)

My useCase is injected on presenter.

GetProductsUseCase has this code:

class GetProductsUseCase (private var productsRepository: ProductsRepository) : UseCase<MutableMap<String, Product>>() {

override suspend fun executeUseCase(): MutableMap<String, Product> {
    val products =productsRepository.getProductsFromApi()
    return products


My BaseUseCase

abstract class UseCase<T> {

    abstract suspend fun executeUseCase(): Any

    fun execute(
        onSuccess: (T) -> Unit,
        genericError: () -> Unit) {
        GlobalScope.launch {
            val result = async {
                try {
                } catch (e: Exception) {
            GlobalScope.launch(Dispatchers.Main) {
                when {
                    result.await() is GenericError -> genericError()
                    else -> onSuccess(result.await() as T)


This useCase call my repository:

    override suspend fun getProductsFromApi(): MutableMap<String, Product> {
    val productsResponse = safeApiCall(
        call = {apiService.getProductsList()},
        error = "Error fetching products"
    productsResponse?.let {
                    return productsMapper.fromResponseToDomain(it)!!
    return mutableMapOf()

Y try to mock my response but test always fails.


class HomePresenterTest {

lateinit var presenter: HomePresenter

lateinit var view: HomeView

lateinit var getProductsUseCase: GetProductsUseCase

lateinit var updateProductsUseCase: UpdateProductsUseCase

private lateinit var products: MutableMap<String, Product>

private val testDispatcher = TestCoroutineDispatcher()
private val testScope = TestCoroutineScope(testDispatcher)

lateinit var productsRepository:ProductsRepositoryImpl

fun setUp() {
    products = ProductsMotherObject.createEmptyModel()
    presenter = HomePresenter(view, getProductsUseCase, updateProductsUseCase, products)

fun after() {


    fun a() = testScope.runBlockingTest {


    private suspend fun setTasksNotAvailable(dataSource: ProductsRepository) {

I don’t know what is happening. The log says:

“Wanted but not invoked:
-> at com.myProject.HomePresenterTest$a$1.invokeSuspend(HomePresenterTest.kt:165)

However, there was exactly 1 interaction with this mock:

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.