为修改其输入并传递给模拟对象的方法创建测试用例
Create test case for the method who modify its input and pass to mock object
我正在使用 Mockito 在 Android 中编写单元测试用例。我被困在一种修改对象并将其传递给模拟方法的方法中,无法理解如何为此
编写单元测试用例
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);
}
}
当我在第 1 行中使用 anyMap() 时,它抛出 - org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
当我在第 1 行中使用 new HashMap<>() 时,它会抛出 NullPointerException
想要为方法 - getRecentLocations 编写测试用例,其中 getRecentLocationFilter 是私有方法
对于 InvalidUseOfMatchersException
,原因可能是您必须使用所有值或所有匹配器。例如:
when(mockLocationInteractor.getUpdatedRecentLocation(anyMap(), any())
我正在使用 Mockito 在 Android 中编写单元测试用例。我被困在一种修改对象并将其传递给模拟方法的方法中,无法理解如何为此
编写单元测试用例 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);
}
}
当我在第 1 行中使用 anyMap() 时,它抛出 - org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
当我在第 1 行中使用 new HashMap<>() 时,它会抛出 NullPointerException
想要为方法 - getRecentLocations 编写测试用例,其中 getRecentLocationFilter 是私有方法
对于 InvalidUseOfMatchersException
,原因可能是您必须使用所有值或所有匹配器。例如:
when(mockLocationInteractor.getUpdatedRecentLocation(anyMap(), any())