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 {

    var instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var repository: ExpensesRepository
    private val dataStore = mock<DataStore>()
    private val expense = Expense(

    private val expenseList = listOf(

    private val dummyKey = "expense_key"

    fun setup() {
        repository = ExpensesRepository(dataStore)

    fun `should get an item with existing key`() {
        val result = repository.get(dummyKey)
        verify(dataStore, Times(1)).getOne<Expense>(dummyKey)

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) {
            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) {

    fun flush() {

Is there something I am doing wrong here?

Read more here:

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: