vendredi 27 novembre 2020

How to mock a Datastore in a testing class?

I'm using this new DataStore library (https://developer.android.com/topic/libraries/architecture/datastore) for caching instead of SharedPreferences and I got in trouble trying to mock it

import androidx.datastore.DataStore
import androidx.datastore.preferences.Preferences
import com.ecpsharing.android.persistencedata.dao.BalancePaymentDao
import io.mockk.clearMocks
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@ExperimentalCoroutinesApi
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class BillingPersistenceDataSourceTest {

    private val dataStore: DataStore<Preferences> = mockk()
    private val balancePaymentDao: BalancePaymentDao = mockk()
    private val ttl = 1000 * 60 * 60 * 12L

    private val billingPersistenceDataSource =
        BillingPersistenceDataSource(dataStore, balancePaymentDao, ttl)

    @AfterEach
    fun clean() {
        clearMocks(dataStore, balancePaymentDao)
    }

    @Test
    fun `setBalancePayments returns the expected result`() = runBlockingTest {
        // TODO
    }
}

If I run this one, a MockkException occurs: enter image description here

I don't really understand how to initialize DataStore object properly to make it work

Aucun commentaire:

Enregistrer un commentaire