Android 为修改其输入并传递给模拟对象的方法创建测试用例
我使用Mockito在Android中编写单元测试用例。我被困在一个方法中,我正在修改对象并将其传递给mock方法,无法理解如何为此编写单元测试用例Android 为修改其输入并传递给模拟对象的方法创建测试用例,android,mocking,mockito,testcase,Android,Mocking,Mockito,Testcase,我使用Mockito在Android中编写单元测试用例。我被困在一个方法中,我正在修改对象并将其传递给mock方法,无法理解如何为此编写单元测试用例 Class LocationViewModel{ private LocationInteractor locationInteractor; LocationViewModel (LocationInteractor locationInteractor){ this.location
Class LocationViewModel{
private LocationInteractor locationInteractor;
LocationViewModel (LocationInteractor locationInteractor){
this.locationInteractor =locationInteractor;
}
@Override
public Single<List<String>> getRecentLocations( LocationViewType locationViewType) {
return locationInteractor.getUpdatedRecentLocation(getRecentLocationFilter(locationViewType),locationViewType);
}
private Map<String, String[]> getRecentLocationFilter(LocationViewType locationViewType) {
LocationFilter locationfilter = new LocationFilter();
if (locationViewType == LocationViewType.DEFAULT_LOCATIONS) {
return locationFilter.getRecentDefaultLocationFilter();
} else if (locationViewType == SETTING_LOCATIONS) {
return locationFilter.getRecentSettingLocationFilter();
} else if (locationViewType == LocationViewType.INVENTORY_LOCATION) {
return locationFilter.getRecentSettingLocationFilter();
} else {
return locationFilter.getRecentCurrentLocationFilter();
}
}
}
Class LocationViewModelTest{
@Mock private LocationInteractorContract mockLocationInteractor;
private LocationViewModelContract locationViewModel;
@Before
public void setUp() {
initMocks(this);
locationViewModel = new LocationViewModel(mockLocationInteractor)
}
@Test
public void getRecentLocationsList_check_for_Null() {
when(mockLocationInteractor.getUpdatedRecentLocation(anyMap(),LocationViewType.SETTING_LOCATIONS)) ......Line 1
.thenReturn(Single.error(NullPointerException::new));
locationViewModel
.getRecentLocations(LocationViewType.SETTING_LOCATIONS)
.test()
.assertFailure(NullPointerException.class);
}
}
Class LocationViewModel{
私人位置交互器位置交互器;
LocationViewModel(LocationInteractor LocationInteractor){
this.LocationInteractitor=LocationInteractitor;
}
@凌驾
公共单getRecentLocations(LocationViewType LocationViewType){
返回locationInteractor.getUpdatedRecentLocation(getRecentLocationFilter(locationViewType),locationViewType);
}
私有映射getRecentLocationFilter(LocationViewType LocationViewType){
LocationFilter LocationFilter=新的LocationFilter();
if(locationViewType==locationViewType.DEFAULT\u位置){
返回locationFilter.getRecentDefaultLocationFilter();
}else if(locationViewType==设置位置){
返回locationFilter.getRecentSettingLocationFilter();
}else if(locationViewType==locationViewType.INVENTORY\u位置){
返回locationFilter.getRecentSettingLocationFilter();
}否则{
返回locationFilter.getRecentCurrentLocationFilter();
}
}
}
类位置ViewModelTest{
@模拟专用位置交互或合同模拟位置交互;
私有位置视图模型合同位置视图模型;
@以前
公共作废设置(){
initMocks(这个);
locationViewModel=新locationViewModel(mockLocationInteractor)
}
@试验
public void getRecentLocationList\u check\u for\u Null(){
当(mockLocationInteractor.getUpdatedRecentLocation(anyMap(),LocationViewType.SETTING_LOCATIONS))第1行
.thenReturn(Single.error(NullPointerException::new));
位置视图模型
.getRecentLocations(LocationViewType.SETTING\u位置)
.test()
.assertFailure(NullPointerException.class);
}
}
当我在第1行中使用anyMap()时,它抛出-org.mockito.exceptions.misusing.invalidUseofMatcherException:
当我在第1行中使用new HashMap()时,它会抛出NullPointerException
要为方法-getRecentLocationFilter是私有方法的getRecentLocationFilter编写测试用例,对于
InvalidUseofMatcherException
,原因可能是您必须使用所有值或所有匹配器。例如:
when(mockLocationInteractor.getUpdatedRecentLocation(anyMap(), any())
尝试不作为-when(mockLocationInteractor.getUpdatedRecentLocation(any(),any())工作。然后返回(Single.error(NullPointerException::new));locationViewModel.getRecentLocations(any()).test().assertFailure(NullPointerException.class);怎么了?你有记录吗?对于
locationViewModel.getRecentLocations(LocationViewType.SETTING\u LOCATIONS)
,您可以保留旧代码。完成,谢谢。实际上,我必须在两个参数上传递any(),谢谢您的帮助