### Question:
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);  }### Answer:
@Test public void whenDataExistsThenReturnedExisting() { PunishmentDataRepository repository = mock(PunishmentDataRepository.class); UUID uuid = UUID.randomUUID(); PunishmentData punishmentData = mock(PunishmentData.class); when(repository.findPunishmentData(eq(uuid))).thenReturn(Optional.of(punishmentData)); PunishmentDataProvider punishmentDataProvider = new PunishmentDataProvider(repository); PunishmentData data = punishmentDataProvider.getPunishmentsFor(uuid); assertNotNull(data); verify(repository, times(0)).save(any()); }
@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)); }