【问题标题】:Create test case for the method who modify its input and pass to mock object为修改其输入并传递给模拟对象的方法创建测试用例
【发布时间】:2019-11-26 13:02:30
【问题描述】:

我正在使用 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 是私有方法

【问题讨论】:

    标签: android mocking mockito testcase


    【解决方案1】:

    对于InvalidUseOfMatchersException,原因可能是您必须使用所有值或所有匹配器。例如:

    when(mockLocationInteractor.getUpdatedRecentLocation(anyMap(), any())
    

    【讨论】:

    • 试过不工作 - when(mockLocationInteractor.getUpdatedRecentLocation(any(),any())) .thenReturn(Single.error(NullPointerException::new)); locationViewModel .getRecentLocations(any()) .test() .assertFailure(NullPointerException.class);
    • 错误是什么?你有日志吗?对于locationViewModel .getRecentLocations(LocationViewType.SETTING_LOCATIONS),您可以保留旧代码。
    • 谢谢。实际上我必须在两个参数上都传递 any() 感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    相关资源
    最近更新 更多