I want to write unit-test to my code. However, while writing it, it seems all I can verify is that one method or the other were called once.
My class is a manager that execute verification flow. It calls many checkers\verifiers - so they have the real logic. How can I test such "verification flow" code?
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);
}
My try for UT. Would you skip testing this code?
@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));
}