How to capture a list of specific type with mockito

后端 未结 8 1590
天命终不由人
天命终不由人 2020-12-12 10:05

Is there a way to capture a list of specific type using mockitos ArgumentCaptore. This doesn\'t work:

ArgumentCaptor> argumen         


        
8条回答
  •  攒了一身酷
    2020-12-12 10:52

    I had the same issue with testing activity in my Android app. I used ActivityInstrumentationTestCase2 and MockitoAnnotations.initMocks(this); didn't work. I solved this issue with another class with respectively field. For example:

    class CaptorHolder {
    
            @Captor
            ArgumentCaptor> captor;
    
            public CaptorHolder() {
                MockitoAnnotations.initMocks(this);
            }
        }
    

    Then, in activity test method:

    HubstaffService hubstaffService = mock(HubstaffService.class);
    fragment.setHubstaffService(hubstaffService);
    
    CaptorHolder captorHolder = new CaptorHolder();
    ArgumentCaptor> captor = captorHolder.captor;
    
    onView(withId(R.id.signInBtn))
            .perform(click());
    
    verify(hubstaffService).authorize(anyString(), anyString(), captor.capture());
    Callback callback = captor.getValue();
    

提交回复
热议问题