模拟给出,实际上这个模拟没有任何互动

时间:2017-06-09 09:51:25

标签: android unit-testing mocking mockito

当我们作为单个测试运行时进行测试,它运行完美,但是当它与其他测试捆绑在一起时,它实际上与此模拟没有任何关联。还要注意一个这样的方法错误,而其他测试通过相同的验证案例。

public class SubjectScheduleScreenPresenterTest {

    @InjectMocks
    private SubjectScheduleScreenPresenter presenter;

    @Mock
    private Context context;

    @Mock
    private SubjectScheduleScreenView view;

    @Mock
    private Payment payment;

    @Mock
    private UserDB userDB;

    @Mock
    private Calendar calendar;
    @Mock
    private SharedPreferences.Editor editor;

    private APIInterface apiInterface;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        apiInterface = NetworkContext.getInstance().getService(APIInterface.class);

    }

    @After
    public void afterMethod(){
        this.presenter = null;
    }

    @Test
    public void gotoPaytmTest(){
        double amount = 2.0, latitude =78.35,longitude = 78.50;
        int subjectId=2,tutorId = 2;

        Mockito.when(userDB.getValueFromSqlite("email",1)).thenReturn("jayakishan100@gmail.com");
        Mockito.when(userDB.getValueFromSqlite("userId",1)).thenReturn("38");

        presenter.gotoPaytm(amount,subjectId,tutorId,latitude,longitude,calendar,editor);
        Call call = apiInterface.getOrderId();

        try {
            Response<SessionBooking> sessionBookingResponse = call.execute();
            SessionBooking sessionBooking =sessionBookingResponse.body();

//            Mockito.verify(payment).callPaytm(sessionBooking.getOrderId(),userDB.getValueFromSqlite("email",1),
//                    amount,context,Integer.parseInt(userDB.getValueFromSqlite("userId",1)),-1,subjectId,latitude,longitude,
//                    calendar,editor);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getSubjectDataTestSuccess(){

        int searchId = 2,from = 1;
        presenter.getSubjectData(searchId,from);

        ScheduleSubject scheduleSubject = new ScheduleSubject(searchId);
        Call call = apiInterface.getSubjectDataForScheduling(scheduleSubject);

        try {
            Response<ScheduleSubject> scheduleSubjectResponse =call.execute();
            ScheduleSubject scheduleSubject1 = (ScheduleSubject) scheduleSubjectResponse.body();
            Mockito.verify(view).setValues(from,scheduleSubject1.getSubjectName().trim(), scheduleSubject1.getPrice());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void getSubjectDataTestFailure(){

        int searchId = -3,from = 1;
        presenter.getSubjectData(searchId,from);

        ScheduleSubject scheduleSubject = new ScheduleSubject(searchId);
        Call call = apiInterface.getSubjectDataForScheduling(scheduleSubject);

        try {
            Response<ScheduleSubject> scheduleSubjectResponse =call.execute();
            ScheduleSubject scheduleSubject1 = (ScheduleSubject) scheduleSubjectResponse.body();
            Mockito.verify(view).setValues(from,scheduleSubject1.getSubjectName().trim(), scheduleSubject1.getPrice());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

getSubjectDataTestSuccess单独的方法在传递其他测试时给出错误。

0 个答案:

没有答案