mockito android.util.Log中的方法isLoggable未被模拟

bksxznpy  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(107)

我想对我的应用程序运行测试,但在when Get Story Success上有一部分测试失败,并显示“Method isLoggable in android.util.Log not mocked”

package com.agilizzulhaq.storyapplicationsubmission.ui.viewmodel

import androidx.arch.core.executor.testing.InstantTaskExecutorRule

import androidx.lifecycle.LiveData

import androidx.lifecycle.MutableLiveData

import androidx.paging.AsyncPagingDataDiffer

import androidx.paging.PagingData

import androidx.paging.PagingSource

import androidx.paging.PagingState

import androidx.recyclerview.widget.ListUpdateCallback

import com.agilizzulhaq.storyapplicationsubmission.DataDummy

import com.agilizzulhaq.storyapplicationsubmission.MainDispatcherRule

import com.agilizzulhaq.storyapplicationsubmission.data.responses.AddStoryResponse

import com.agilizzulhaq.storyapplicationsubmission.data.responses.ListStoryItem

import com.agilizzulhaq.storyapplicationsubmission.data.responses.StoryResponse

import com.agilizzulhaq.storyapplicationsubmission.data.story.StoryRepository

import com.agilizzulhaq.storyapplicationsubmission.getOrAwaitValue

import com.agilizzulhaq.storyapplicationsubmission.ui.adapter.StoryAdapter

import kotlinx.coroutines.Dispatchers

import kotlinx.coroutines.ExperimentalCoroutinesApi

import kotlinx.coroutines.test.runTest

import org.junit.Assert

import org.junit.Before

import org.junit.Rule

import org.junit.Test

import org.junit.runner.RunWith

import org.mockito.Mock

import org.mockito.Mockito

import org.mockito.junit.MockitoJUnitRunner

import java.io.File

@ExperimentalCoroutinesApi

@RunWith(MockitoJUnitRunner::class)

class MainViewModelTest {

    @get:Rule

    val instantExecutorRule = InstantTaskExecutorRule()

    @get:Rule

    val mainDispatcherRules = MainDispatcherRule()

    @Mock

    private lateinit var storyRepository: StoryRepository

    private lateinit var mainViewModel: MainViewModel

    private val dummyStory = DataDummy.generateDummyListStoryItem()

    private val dummyStoryResponse = DataDummy.generateDummyStoryResponse()

    private val dummyAddStoryResponse = DataDummy.generateDummyAddStoryResponse()

    private val token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"

    @Before

    fun setUp() {

        mainViewModel = MainViewModel(storyRepository)

    }

    @Test

    fun `when Get Story Success`() = runTest {

        val data: PagingData<ListStoryItem> = StoryPagingSource.snapshot(dummyStory)

        val expectedQuote = MutableLiveData<PagingData<ListStoryItem>>()

        expectedQuote.value = data

        Mockito.`when`(storyRepository.getStory(token)).thenReturn(expectedQuote)

        val mainViewModel = MainViewModel(storyRepository)

        val actualQuote: PagingData<ListStoryItem> = mainViewModel.getStory(token).getOrAwaitValue()

        val differ = AsyncPagingDataDiffer(

            diffCallback = StoryAdapter.DIFF_CALLBACK,

            updateCallback = noopListUpdateCallback,

            workerDispatcher = Dispatchers.Main,

        )

        differ.submitData(actualQuote)

        Assert.assertNotNull(differ.snapshot())

        Assert.assertEquals(dummyStory.size, differ.snapshot().size)

        Assert.assertEquals(dummyStory[0], differ.snapshot()[0])

    }

    @Test

    fun `when Post Story`() {

        val file = File("image")

        val expectedResponse = MutableLiveData<AddStoryResponse>()

        expectedResponse.postValue(dummyAddStoryResponse)

        Mockito.`when`(storyRepository.addStory(token, file, "description", null, null)).thenReturn(expectedResponse)

        val actualResponse = mainViewModel.addStory(token, file, "description", null, null).getOrAwaitValue()

        Mockito.verify(storyRepository).addStory(token, file, "description", null, null)

        Assert.assertNotNull(actualResponse)

        Assert.assertEquals(actualResponse, expectedResponse.value)

    }

    @Test

    fun `when Get List Story Maps`() {

        val expectedResponse = MutableLiveData<StoryResponse>()

        expectedResponse.postValue(dummyStoryResponse)

        Mockito.`when`(storyRepository.getListMapsStory(token)).thenReturn(expectedResponse)

        val actualResponse = mainViewModel.getListMapsStory(token).getOrAwaitValue()

        Mockito.verify(storyRepository).getListMapsStory(token)

        Assert.assertNotNull(actualResponse)

        Assert.assertEquals(expectedResponse.value, actualResponse)

    }

}

class StoryPagingSource : PagingSource<Int, LiveData<List<ListStoryItem>>>() {

    companion object {

        fun snapshot(items: List<ListStoryItem>): PagingData<ListStoryItem> {

            return PagingData.from(items)

        }

    }

    override fun getRefreshKey(state: PagingState<Int, LiveData<List<ListStoryItem>>>): Int {

        return 0

    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, LiveData<List<ListStoryItem>>> {

        return LoadResult.Page(emptyList(), 0, 1)

    }

}

val noopListUpdateCallback = object : ListUpdateCallback {

    override fun onInserted(position: Int, count: Int) {}

    override fun onRemoved(position: Int, count: Int) {}

    override fun onMoved(fromPosition: Int, toPosition: Int) {}

    override fun onChanged(position: Int, count: Int, payload: Any?) {}

}

字符串

jjjwad0x

jjjwad0x1#

尝试使用这个:

testOptions {
    unitTests.isReturnDefaultValues = true
}

字符串

相关问题