@Test public void whenDataDoesNotExistThenCreatedNew() { PunishmentDataRepository repository = mock(PunishmentDataRepository.class); UUID uuid = UUID.randomUUID(); when(repository.findPunishmentData(eq(uuid))).thenReturn(Optional.empty()); PunishmentDataProvider provider = new PunishmentDataProvider(repository); PunishmentData data = provider.getPunishmentsFor(uuid); assertNotNull(data); verify(repository).save(eq(data)); }
public PunishmentData getPunishmentsFor(UUID uuid) { return punishmentDataRepository .findPunishmentData(uuid) .orElseGet(() -> { PunishmentData punishmentData = new PunishmentData(uuid); punishmentDataRepository.save(punishmentData); return punishmentData; }); }
PunishmentDataProvider { public PunishmentData getPunishmentsFor(UUID uuid) { return punishmentDataRepository .findPunishmentData(uuid) .orElseGet(() -> { PunishmentData punishmentData = new PunishmentData(uuid); punishmentDataRepository.save(punishmentData); return punishmentData; }); } }
PunishmentDataProvider { public PunishmentData getPunishmentsFor(UUID uuid) { return punishmentDataRepository .findPunishmentData(uuid) .orElseGet(() -> { PunishmentData punishmentData = new PunishmentData(uuid); punishmentDataRepository.save(punishmentData); return punishmentData; }); }  }
PunishmentDataProvider { public PunishmentData getPunishmentsFor(UUID uuid) { return punishmentDataRepository .findPunishmentData(uuid) .orElseGet(() -> { PunishmentData punishmentData = new PunishmentData(uuid); punishmentDataRepository.save(punishmentData); return punishmentData; }); }  PunishmentData getPunishmentsFor(UUID uuid); }
PunishmentDataProvider { public PunishmentData getPunishmentsFor(UUID uuid) { return punishmentDataRepository .findPunishmentData(uuid) .orElseGet(() -> { PunishmentData punishmentData = new PunishmentData(uuid); punishmentDataRepository.save(punishmentData); return punishmentData; }); }  PunishmentData getPunishmentsFor(UUID uuid);  }