在方法单元测试中调用方法

时间:2017-06-06 09:32:36

标签: android unit-testing junit mockito android-testing

我有一位主持人接受测试。 Presenter包含一个方法,该方法在某些成功返回时调用另一个方法。现在我如何检查成功返回是否调用该方法?

这是示例代码,它只是验证一个非模拟对象的方法 tutorProfileDataReceived(tutorProfiledata)是我需要确保调用onResponse的方法。

现在这给了实际上,与这个模拟没有交互。

Call call = apiInterface.getTutorProfile(tutorProfile);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                TutorProfile tutorProfiledata = (TutorProfile) response.body();
                tutorProfileDataReceived(tutorProfiledata);
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                Log.d(TAG,"getting suggestions call cancel");
                call.cancel();
            }
        });

测试工作......

@Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        apiInterface = NetworkContext.getInstance().getService(APIInterface.class);
        presenter = Mockito.spy(new TutorProfilePresenter(viewMock , contextMock));

    }

    @Test

        public void getTutorDataTest(){
            int searchId = 2;

            TutorProfile profileSender = new TutorProfile(searchId);
            Call<TutorProfile> call = apiInterface.getTutorProfile(profileSender);

            try {
                Response<TutorProfile> response = call.execute();
                TutorProfile profileReceiver = response.body();

                Mockito.verify(presenter).tutorProfileDataReceived(profileReceiver);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

0 个答案:

没有答案