### Question:
AccountingAdapter { public Optional<String> bookCharges( final Map<String, BigDecimal> balanceAdjustments, final DesignatorToAccountIdentifierMapper designatorToAccountIdentifierMapper, final String note, final String transactionDate, final String message, final String transactionType) { final JournalEntry journalEntry = getJournalEntry( balanceAdjustments, designatorToAccountIdentifierMapper, note, transactionDate, message, transactionType, UserContextHolder.checkedGetUser()); if (journalEntry.getCreditors().isEmpty() && journalEntry.getDebtors().isEmpty()) return Optional.empty(); while (true) { try { final String transactionUniqueifier = RandomStringUtils.random(26, true, true); journalEntry.setTransactionIdentifier(formulateTransactionIdentifier(message, transactionUniqueifier)); ledgerManager.createJournalEntry(journalEntry); return Optional.of(transactionUniqueifier); } catch (final JournalEntryAlreadyExistsException ignore) { } } } @Autowired  AccountingAdapter(@SuppressWarnings("SpringJavaAutowiringInspection") final LedgerManager ledgerManager,
                           final AccountingListener accountingListener,
                           @Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger); Optional<String> bookCharges(
      final Map<String, BigDecimal> balanceAdjustments,
      final DesignatorToAccountIdentifierMapper designatorToAccountIdentifierMapper,
      final String note,
      final String transactionDate,
      final String message,
      final String transactionType); Optional<LocalDateTime> getDateOfOldestEntryContainingMessage(final String accountIdentifier,
                                                                       final String message); BigDecimal sumMatchingEntriesSinceDate(final String accountIdentifier, final LocalDate startDate, final String message); Account getAccount(final String accountIdentifier); String createLedger(
      final String customerIdentifier,
      final String groupName,
      final String parentLedger); String createProductAccountForLedgerAssignment(
      final String productIdentifier,
      final String accountDesignator,
      final String ledgerIdentifier); String createOrFindCaseAccountForLedgerAssignment(
      final String customerIdentifier,
      final AccountAssignment ledgerAssignment,
      final BigDecimal currentBalance); static Set<String> accountAssignmentsRequiredButNotProvided(
          final Set<AccountAssignment> accountAssignments,
          final Stream<ChargeDefinition> chargeDefinitionEntities); static Set<String> getRequiredAccountDesignators(final Stream<ChargeDefinition> chargeDefinitionEntities); Set<String> accountAssignmentsMappedToNonexistentAccounts(final Set<AccountAssignment> accountAssignments); boolean accountAssignmentRepresentsRealAccount(final AccountAssignment accountAssignment);  }### Answer:
@Test public void journalEntryCreationFailsBecauseIdentifierAlreadyExistsShouldCauseRetry() { final LedgerManager ledgerManagerMock = Mockito.mock(LedgerManager.class); final AccountingAdapter testSubject = new AccountingAdapter(ledgerManagerMock, null, null); final Map<String, BigDecimal> balanceAdjustments = new HashMap<>(); balanceAdjustments.put("a", BigDecimal.ONE); balanceAdjustments.put("b", BigDecimal.ONE.negate()); final DesignatorToAccountIdentifierMapper designatorToAccountIdentifierMapper = Mockito.mock(DesignatorToAccountIdentifierMapper.class); Mockito.doReturn("a1").when(designatorToAccountIdentifierMapper).mapOrThrow("a"); Mockito.doReturn("b1").when(designatorToAccountIdentifierMapper).mapOrThrow("b"); Mockito.doThrow(JournalEntryAlreadyExistsException.class) .doThrow(JournalEntryAlreadyExistsException.class) .doNothing() .when(ledgerManagerMock).createJournalEntry(Matchers.anyObject()); UserContextHolder.setAccessToken("blah", "blah"); testSubject.bookCharges( balanceAdjustments, designatorToAccountIdentifierMapper, "", "", "x", ""); Mockito.verify(ledgerManagerMock, Mockito.atLeast(3)).createJournalEntry(Matchers.anyObject()); }