Cloud Stack Ninja

I am trying to test a repository ExpensesRepository

class ExpensesRepository @Inject constructor(
    private val store: DataStore
) : AbstractRepository<Expense> {

    override fun get(offset: Int, limit: Int): List<Expense?> =
        store.getAll(offset, limit)

    override fun get(key: String): Expense? = store.getOne(key = key)

    override fun create(input: Expense) =
        store.create(key = input.key, element = input)


    override fun delete(key: String) = store.remove(key = key)

    override fun update(
        key: String,
        update: Expense
    ): Expense =
        store.update(key = key, element = update)

    override fun removeAll() = store.flush()
}

In the test suite I have mocked the store parameter using mockito, then I stubbed that mock's method getOne(String) to return an expense object from my fixtures.

Here is my test suite

class ExpensesRepositoryTest {

    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var repository: ExpensesRepository
    private val dataStore = mock<DataStore>()
    private val expense = Expense(
        "key",
        1,
        "name",
        "date",
        12.12
    )

    private val expenseList = listOf(
        expense,
        expense,
        expense
    )

    private val dummyKey = "expense_key"

    @Before
    fun setup() {
        repository = ExpensesRepository(dataStore)
    }

    @Test
    fun `should get an item with existing key`() {
        whenever(dataStore.getOne<Expense>(dummyKey)).thenReturn(expense)
        val result = repository.get(dummyKey)
        verify(dataStore, Times(1)).getOne<Expense>(dummyKey)
        assertThat(result).isEqualTo(expense)
    }
}

When I run the test it fails cuz of NullPointerException and that's when the DataStore calls the function getOne(String) that I supposedly stubbed earlier. In case it helps here is the DataStore class

class DataStore(val store: Persistence, val gson: Gson) {

    inline fun <reified R : Persistable> getAll(
        offset: Int,
        limit: Int
    ): List<R?> =
        store.get(offset, limit).map { it.maybeMapTo<R>(mapper = gson) }

    inline fun <reified R : Persistable> getOne(key: String): R? =
        store.get(key).maybeMapTo<R>(mapper = gson)

    fun create(key: String, element: Persistable) {
        store.create(
            key = key,
            element = when (store) {
                is JsonBased -> gson.toJson(element)
                else -> element
            }
        )
    }

    inline fun <reified R : Persistable> update(
        key: String,
        element: R
    ): R {
        return store.update(key, element) as R
    }

    fun remove(key: String) {
        store.remove(key)
    }

    fun flush() {
        store.flush()
    }
}

Is there something I am doing wrong here?



Read more here: https://stackoverflow.com/questions/64393263/npe-when-stubbing-a-mocks-method-with-mockito

Content Attribution

This content was originally published by AouledIssa at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: