### Question:
ScheduledChargesService { Optional<ChargeRange> findChargeRange(final String productIdentifier, final ChargeDefinition chargeDefinition) { if ((chargeDefinition.getForSegmentSet() == null) || (chargeDefinition.getFromSegment() == null) || (chargeDefinition.getToSegment() == null)) return Optional.empty(); final List<BalanceSegmentEntity> segmentSet = balanceSegmentRepository.findByProductIdentifierAndSegmentSetIdentifier(productIdentifier, chargeDefinition.getForSegmentSet()) .sorted(Comparator.comparing(BalanceSegmentEntity::getLowerBound)) .collect(Collectors.toList()); final Map<String, Segment> segments = Stream.iterate(0, i -> i + 1).limit(segmentSet.size()) .map(i -> new Segment( segmentSet.get(i).getSegmentIdentifier(), segmentSet.get(i).getLowerBound(), Optional.ofNullable(i + 1 < segmentSet.size() ? segmentSet.get(i + 1).getLowerBound() : null) )) .collect(Collectors.toMap(x -> x.identifier, x -> x)); final Optional<Segment> fromSegment = Optional.ofNullable(segments.get(chargeDefinition.getFromSegment())); final Optional<Segment> toSegment = Optional.ofNullable(segments.get(chargeDefinition.getToSegment())); if (!fromSegment.isPresent() || !toSegment.isPresent()) return Optional.empty(); return Optional.of(new ChargeRange(fromSegment.get().getLowerBound(), toSegment.get().getUpperBound())); } @Autowired  ScheduledChargesService(
      final ChargeDefinitionService chargeDefinitionService,
      final BalanceSegmentRepository balanceSegmentRepository); List<ScheduledCharge> getScheduledCharges(
      final String productIdentifier,
      final @Nonnull List<ScheduledAction> scheduledActions);  }### Answer:
@Test public void findChargeRange() throws Exception { final ChargeDefinitionService chargeDefinitionServiceMock = Mockito.mock(ChargeDefinitionService.class); final BalanceSegmentRepository balanceSegmentRepositoryMock = Mockito.mock(BalanceSegmentRepository.class); Mockito.doReturn(testCase.balanceSegmentEntities.stream()) .when(balanceSegmentRepositoryMock) .findByProductIdentifierAndSegmentSetIdentifier(PRODUCT_IDENTIFIER, SEGMENT_SET_IDENTIFIER); final ScheduledChargesService testSubject = new ScheduledChargesService(chargeDefinitionServiceMock, balanceSegmentRepositoryMock); final ChargeDefinition chargeDefinition = new ChargeDefinition(); chargeDefinition.setForSegmentSet(SEGMENT_SET_IDENTIFIER); chargeDefinition.setFromSegment(testCase.fromSegment); chargeDefinition.setToSegment(testCase.toSegment); final Optional<ChargeRange> result = testSubject.findChargeRange(PRODUCT_IDENTIFIER, chargeDefinition); Assert.assertEquals(testCase.expectedResult, result); }