如何测试“验证流程”代码?你会测试这个或者只是个别的跳棋吗?

时间:2015-04-02 10:43:01

标签: java unit-testing testing mocking mockito

我想将单元测试写入我的代码

然而,在编写它时,我似乎只能验证一个方法或另一个方法被调用一次。

我的班级是执行验证流程的经理。

它会调用许多检查器\验证器 - 因此它们具有真实的逻辑。

您如何建议我测试这样的“验证流程”代码?

我的代码:

public AlternativeShortTotalChecker(SegmentsIdsChecker segmentsIdsChecker, InstructionsChecker instructionsChecker,
                                    RouteNameChecker routeNameChecker, AlternativeDistanceChecker
                                            alternativeDistanceChecker, AlternativeTimeChecker
                                            alternativeTimeChecker,
                                    IDeltaFromBaselineGenerator deltaFromBaselineGenerator,
                                    AStarVisitedChecker aStarVisitedChecker, GlobalSettings globalSettings) {
    this.segmentsIdsChecker = segmentsIdsChecker;
    this.instructionsChecker = instructionsChecker;
    this.routeNameChecker = routeNameChecker;
    this.alternativeDistanceChecker = alternativeDistanceChecker;
    this.alternativeTimeChecker = alternativeTimeChecker;
    this.deltaFromBaselineGenerator = deltaFromBaselineGenerator;
    this.aStarVisitedChecker = aStarVisitedChecker;
    this.globalSettings = globalSettings;
}


public List<String> checkAllAlternativesShort(CompleteRoutingResponseShort baselineRoutingResponse,
                                              CompleteRoutingResponseShort
                                                      routingResponse) {
    int numOfAlts = routingResponse.alternatives.size();
    List<String> testFailureMessages = new ArrayList<>();
    for (int i = 0; i < numOfAlts; i++) {
        ResponseShort currentResponse = getAlternativeShort(routingResponse, i).response;
        ResponseShort baselineResponse = getAlternativeShort(baselineRoutingResponse, i).response;
        AlternativeShort alternativeShort = getAlternativeShort(routingResponse, i);
        String testFailureMessage = checkAlternative(alternativeShort.alternative_regression_id, currentResponse,
                baselineResponse);
        testFailureMessages.add(testFailureMessage);
    }
    return testFailureMessages;
}

private AlternativeShort getAlternativeShort(CompleteRoutingResponseShort routingResponse, int i) {
    return routingResponse.alternatives.get(i);
}

private String checkAlternative(String currentAltId, ResponseShort currentResponse,
                                ResponseShort baselineResponse) {
    IndividualStat individualStat = new IndividualStat(currentAltId);
    aStarVisitedChecker.setForAltDiff(globalSettings.astarVisited_altDiffHighThreshold);
    individualStat.aStarVisitedDelta = aStarVisitedChecker.checkAStarCountPerAlt(currentResponse, baselineResponse);

    individualStat.segmentsIdsDelta = segmentsIdsChecker.checkSegmentsIdsUnorderedIdentity(currentResponse,
            baselineResponse);
    switch (individualStat.segmentsIdsDelta.delta) {
        case IDENTICAL: {
            individualStat.areInstructionsIdentical = instructionsChecker.checkTotalInstructionIdentity
                    (currentResponse, baselineResponse);
            individualStat.areRouteNamesIdentical = routeNameChecker.checkRouteNameIdentity(currentResponse,
                    baselineResponse);
            break;
        }
        case SMALL:
        case LARGE: {
            individualStat.distancesDelta = alternativeDistanceChecker.checkTotalDistanceIdentity
                    (currentResponse, baselineResponse);
            individualStat.timesDelta = alternativeTimeChecker.checkTotalTimeIdentity(currentResponse,
                    baselineResponse);
            break;
        }
    }
    return deltaFromBaselineGenerator.addDeltaForSpecificAlternative(individualStat);
}

我尝试UT。你会跳过测试这段代码吗?

@Test
public void checkAltWith2() throws Exception {
    DescriptiveDelta descriptiveDelta = new DescriptiveDelta();
    descriptiveDelta.delta = Delta.IDENTICAL;
    when(segmentsIdsCheckerMock.checkSegmentsIdsUnorderedIdentity(any(ResponseShort.class), any(ResponseShort
            .class)))
            .thenReturn(descriptiveDelta);

    AlternativeShortTotalChecker alternativeShortTotalChecker =
            new AlternativeShortTotalCheckerBuilder()
                    .setAlternativeDistanceChecker(alternativeDistanceCheckerMock)
                    .setAlternativeTimeChecker(alternativeTimeCheckerMock)
                    .setAStarVisitedChecker(aStarVisitedCheckerMock)
                    .setDeltaFromBaselineGenerator(deltaFromBaselineGeneratorMock)
                    .setGlobalSettings(globalSettings)
                    .setInstructionsChecker(instructionsCheckerMock)
                    .setRouteNameChecker(routeNameCheckerMock)
                    .setSegmentsIdsChecker(segmentsIdsCheckerMock)
                    .createAlternativeShortTotalChecker();


    CompleteRoutingResponseShort currentResponse = routingResponsesInMemoryFactory.getOneAlternative();
    CompleteRoutingResponseShort baselineResponse = routingResponsesInMemoryFactory.getOneAlternative();

    alternativeShortTotalChecker.checkAllAlternativesShort(currentResponse, baselineResponse);

    verify(aStarVisitedCheckerMock).checkAStarCountPerAlt(any(ResponseShort.class), any(ResponseShort.class));
    verify(segmentsIdsCheckerMock).checkSegmentsIdsUnorderedIdentity(any(ResponseShort.class), any(ResponseShort
            .class));
    verify(instructionsCheckerMock).checkTotalInstructionIdentity(any(ResponseShort.class), any(ResponseShort
            .class));
    verify(routeNameCheckerMock).checkRouteNameIdentity(any(ResponseShort.class), any(ResponseShort.class));

    verify(alternativeDistanceCheckerMock, times(0)).checkTotalDistanceIdentity(any(ResponseShort.class), any
            (ResponseShort.class));
    verify(alternativeTimeCheckerMock, times(0)).checkTotalTimeIdentity(any(ResponseShort.class), any
            (ResponseShort.class));
}

0 个答案:

没有答案