父模拟类测试失败

时间:2019-08-20 16:04:42

标签: android android-espresso android-testing

我有DetailViewModel和CoreDetailViewModel`,DetailViewModel扩展了CoreDetailViewModel

class DetailViewModel @Inject constructor(xxxapp: XXXApp) : CoreDetailViewModel(xxxapp), Injectable


abstract class CoreDetailViewModel(val context: Application) : AndroidViewModel(context){

  private val liveEventActionTitleMutableData = MutableLiveData<String>()
  val liveEventActionTitleData: LiveData<String>
    get() = liveEventActionTitleMutableData
}

在测试类中,我模拟DetailViewModel

viewModel = Mockito.mock(DetailViewModel::class.java)
 Mockito.`when`(viewModel.liveEventActionTitleData).thenReturn(data)````

and my test fails saying


org.mockito.exceptions.misusing.MissingMethodInvocationException:     when()需要一个参数,该参数必须是“模拟的方法调用”。     例如:         when(mock.getArticles())。thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.```

当我将liveEventActionTitleData移到DetailViewModel时,它就会起作用 我想知道为什么CoreDetailViewModel是抽象的?

0 个答案:

没有答案