为什么我得到org.mockito.exceptions.misusing.CannotStubVoidMethodWithReturnValue?

时间:2018-10-05 18:42:20

标签: android unit-testing mockito

我正在尝试在Android上学习Mockito,并使用nhaarman的mockito-kotlin库。我收到一个奇怪的Mockito错误,内容为:

“ org.mockito.exceptions.misusing.CannotStubVoidMethodWithReturnValue: 'execute'是一个 void方法,并且不能加上返回值

奇怪的是,我没有对此类进行插桩。这是有问题的单元测试。

@RunWith(JUnit4::class)
class PoiListViewModelTest {

    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    //Mocks
    @Mock lateinit var getPois: GetPois
    @Mock lateinit var mapper: PoiMapper
    @Mock lateinit var observer: Observer<List<Poi_Presentation>>

    // Class being tested
    lateinit var poiListViewModel: PoiListViewModel

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)
        poiListViewModel = PoiListViewModel(getPois,mapper)
    }

    private fun stubMapper(poi_Domain: Poi_Domain, poiPresentation: Poi_Presentation) {
        `when`(mapper.mapToPresentation(poi_Domain)).thenReturn(poiPresentation)
    }

    @Test
    fun fetchAllPoisExecutesUseCaseOnStart(){
        verify(getPois, times(1)).execute(any(), eq(null))
    }

    @Test
    fun poiSubscriberOnNextMapsCorrectly(){
        val poiDomains = TestDataFactory.makePoiDomainList(2)
        val poiPresentations = TestDataFactory.makePoiPresentationList(2)
        stubMapper(poiDomains[0],poiPresentations[0])

        poiListViewModel.getAllPois().observeForever(observer)    
        poiListViewModel.PoiSubscriber().onNext(poiDomains)
        assertEquals(poiListViewModel.getAllPois().value,poiPresentations)
    }
}

这是我的课程正在测试:

open class PoiListViewModel @Inject constructor(
        private val getPois: GetPois,
        private val mapper: PoiMapper
) : ViewModel() {

    private val poiData: MutableLiveData<List<Poi_Presentation>> = MutableLiveData()

    init {
       fetchAllPois()
    }

    fun getAllPois(): LiveData<List<Poi_Presentation>> {
        return poiData
    }

    fun fetchAllPois() {
        getPois.execute(PoiSubscriber())
    }

    override fun onCleared() {
        getPois.dispose()
        super.onCleared()
    }

    inner class PoiSubscriber: DisposableObserver<List<Poi_Domain>>(){
        override fun onComplete() {
            Log.i("poiSubscriber: ", "completed")
        }

        override fun onNext(data: List<Poi_Domain>) {
            poiData.postValue(data.map {
                mapper.mapToPresentation(it)
            })
        }

        override fun onError(e: Throwable) {
            Log.e("Obervable error: ", e.localizedMessage )
        }
    }
}

这是一个简短的单元测试,您可以看到我试图执行存根,所以Mockito为什么会给我这个错误?创建我的测试类时,execute方法会触发,但是我使用了一个模拟对象(getPois),因此该方法在默认情况下不应该执行任何操作?奇怪的是,当我尝试存根另一个类PoiMapper时发生错误。这是该课程:

open class PoiMapper @Inject constructor() {
    fun mapToPresentation(domainModel: Poi_Domain): Poi_Presentation {
        return Poi_Presentation(domainModel.id,domainModel.name,domainModel.description,
                domainModel.img_url,domainModel.latitude,domainModel.longitude,domainModel.imgFocalpointX,
                domainModel.imgFocalpointY,domainModel.collection,domainModel.collectionPosition,
                domainModel.release,domainModel.stampText)
    }
}

此方法确实具有返回类型,因此它不应成为问题的一部分。我希望有人可以向我解释当我感到困惑时发生了什么事。这与我在lambda中调用此方法有关吗?

0 个答案:

没有答案