Mockito错误-“此模拟的互动次数为零”

时间:2019-06-05 01:36:00

标签: android mockito

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    // Send SiteCatalyst data

    //create trigger to adobe analytics when the view visible
    mMeasurement = MeasurementWrapper.create(MeasurementEnum.SPLASH);
    mMeasurement.trackSplashState();
  

在这里我在启动屏幕的onCreate方法中调用trackSplashState。

   @Override
    public void trackSplashState() {
        HashMap<String, Object> data = createCommonData();
        MeasureServiceImpl.StartStatus status = mMeasureService.getLaunchStatus();
        switch (status) {
            case INSTALL: {
                data.put("appevent.install", "install");
                break;
            }
            case LAUNCH: {
                data.put("appevent.launch", "launch");
                break;
            }
            case UPDATE: {
                data.put("appevent.update", "update");
                break;
            }
        }
        mAnalyticsService.trackState(mType, data);
    }
  

MeassurementWrapper.java类中的方法功能

@Override
public void trackState(MeasurementEnum mType, HashMap<String, Object> data, String... additionalData) {
    try {
        String stateName = MeasurementWrapper.DEVICE_NAME + ":" + String.format(mType.getName(), additionalData);
        // Check last Adobe Analytic page value is same to the current page value if so avoiding it send to the Adobe analytics tracking
        if (!isPageAndUrlMatching(mType,data)) {
            Analytics.trackState(stateName, data);
        }
    } catch (MissingFormatArgumentException e) {
        Log.e(this.getClass().getName(), e.getMessage());
    }
}
  

service(AnalyticsServiceImpl)内部的trackState方法的实现。

    <-------- Test Class -------------->
@Config(constants = BuildConfig.class, sdk = TestConfig.SDK)
@RunWith(RobolectricTestRunner.class)
public class AdobeAnalyticsTriggerTest {

    private ArgumentCaptor<MeasurementEnum> enumArgumentCaptor;

    @Module(includes = TestAppModule.class, injects = AdobeAnalyticsTriggerTest.class, overrides = true)
    static class TestModule {}

    @Inject
    Context context;

    @Captor
    ArgumentCaptor<HashMap<String, Object>> data;

    @Captor
    ArgumentCaptor<String[]> varargs;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        Services.initialize(new AdobeAnalyticsTriggerTest.TestModule()).inject(this);
        enumArgumentCaptor = ArgumentCaptor.forClass(MeasurementEnum.class);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void shouldTrackSplashscreen_afterOnCreate(){
        SplashActivity splashActivity = Robolectric.buildActivity(SplashActivity.class).create().get();
        Measurement measurement = mock(splashActivity.mMeasurement);
        verify(((MeasurementWrapper) measurement).mAnalyticsService, times(1)).trackState(enumArgumentCaptor.capture(), data.capture());

    }

    /**
     * Mocks measurement (so stuff is not sent to sitecatalyst)
     * @return measurement
     */
    private Measurement mock(Measurement measurement) {
        MeasurementWrapper wrapper = (MeasurementWrapper) measurement;

        wrapper.mAnalyticsService = Mockito.spy(new AnalyticsServiceImpl());

        // overwrite sendTrackAction() to prevent logs being sent somewhere
        doAnswer(invocationOnMock -> null).when(wrapper.mAnalyticsService).trackAction(any(MeasurementEnum.class), (HashMap) anyMapOf(String.class, Objects.class));

        // overwrite sendStateAction() to prevent logs being sent somewhere
        doAnswer(invocationOnMock -> null).when(wrapper.mAnalyticsService).trackState(any(MeasurementEnum.class), (HashMap) anyMapOf(String.class, Objects.class));

        return wrapper;
    }
  

我需要在启动屏幕调用onCreate方法时测试( shouldTrackTrackSplashscreen_afterOnCreate )TrackState方法。但是问题是,我总是从Mockito中得到一个错误,实际上,该模拟游戏的互动为零。有人可以帮助我解决此问题。这将对我有很大帮助。谢谢和问候。

1 个答案:

答案 0 :(得分:0)

您需要在模拟和验证部分之间添加您的测试代码,示例如下所示。

当前在模拟上没有完成任何方法调用,因此您会从模拟中得到错误消息。

tb.SelectionStart = 0; //before first char
tb.SelectionLength = 0; //don't select any chars