将Retrofit回调添加到单元测试

时间:2018-09-20 20:51:39

标签: android callback mockito retrofit

我正在尝试使用Mockito进行一个相当简单的单元测试,但是在回调方面遇到了一些麻烦。我一直在探索几种替代方法,其中一些看起来不错,包括协同例程以及使用Mockito的do answer方法。我还没有找到一个好的解决方案,有什么想法吗?

这是我的考试;

    //Test for add 5 minutes
@Test
public void testWithArgumentWithDummyReturnObject() throws ParseException {
    ParkingMeter testZone = new ParkingMeter();
    testZone.setZoneId("21788");
    testZone.setMeterName("Train Lot");

    Date startDate = dateFormat.parse("09/10/2018 1:00 PM");
    Date endDate = dateFormat.parse("09/10/2018 1:05 PM");
    Date expectedOutputDate = dateFormat.parse("09/10/2018 1:05 PM");

    CurrentSessionsResponse fakeResponse = new CurrentSessionsResponse();
    CurrentSession fakeSession = new CurrentSession("fake","","");
    fakeSession.zone = "Fake 1";

    ArrayList<CurrentSession> sessionlist = new ArrayList<>();
    sessionlist.add(fakeSession);
    fakeResponse.parkingSessions = sessionlist;

    MockRepository mockRepo = mock(MockRepository.class);

    doReturn(fakeResponse).when(mockRepo).getCurrentSessions();


    ExtendedVariableRateUtil variableRateUtil = new ExtendedVariableRateUtil();
    variableRateUtil.init(testZone,"", 360, mockRepo);
    variableRateUtil.setTime(5);

    assertThat(variableRateUtil.getDidJump(), is(false));
    assertThat(variableRateUtil.getCurrentCustomerTime(), is(expectedOutputDate));
}

这是正在测试的类中的相关方法:

    fun init(zone: ParkingMeter, plateNumber: String, extendedVariableRateMaxDayMinutes: Int, repository: ParkSmarterRepository) {
    this.selectedZone = zone
    this.plateNumber = plateNumber
    this.extendedVariableRateMaxDayMinutes = extendedVariableRateMaxDayMinutes

    if (selectedZone.maxTime < extendedVariableRateMaxDayMinutes * 24 * 60) {
        //extendedVariableRateMaxDayJump  = selectedZone.maxTime!
    }

    repository.getCurrentSessions { list, message ->
        if (list?.size!! > 0) {
            list.forEach {
                if (it.zoneID == selectedZone.ZoneId && it.vehicle == plateNumber) {
                    isNewSession = false
                    var startDate = it.startTime
                    var endDate = it.endTime
                } else {
                    isNewSession = true
                }
            }
        }
        setCurrentCustomerTime()
    }
}

改造后的回调函数:

fun getCurrentSessions(callback: (currentSessions: List<CurrentSession>?, message: String?) -> Unit) {
    val call = ParkSmarter.apiInterface.getCurrentSessions()
    call.enqueue(object : Callback<CurrentSessionsResponse> {
        override fun onResponse(call: Call<CurrentSessionsResponse>, responseParkSmarter: Response<CurrentSessionsResponse>?) {
            try {
                callback(responseParkSmarter?.body()?.parkingSessions, responseParkSmarter?.body()?.psResponse?.Message)
            } catch (ex: Exception) {
                ex.printStackTrace()
            }
        }

        override fun onFailure(call: Call<CurrentSessionsResponse>, t: Throwable?) {
            callback(null, "Network Error")
        }
    })
}

和改造电话

    @Headers(HEADERS)
@GET("/api/ParkingSession")
Call<CurrentSessionsResponse> getCurrentSessions();

0 个答案:

没有答案