### Question:
BackupServiceImpl implements BackupService { @Override public BackupInfoRecord getBackup(String userName, String id) { return backupDao.getBackup(userName, id).orElseThrow(() -> new ResourceNotFoundException( String.format("Backup with id %s was not found for user %s", id, userName))); }  @Override String createBackup(EnvBackupDTO dto, UserInfo user); @Override void updateStatus(EnvBackupDTO dto, String user, EnvBackupStatus status); @Override List<BackupInfoRecord> getBackups(String userName); @Override BackupInfoRecord getBackup(String userName, String id);  }### Answer:
@Test public void getBackup() { BackupInfoRecord biRecord = mock(BackupInfoRecord.class); when(backupDao.getBackup(anyString(), anyString())).thenReturn(Optional.of(biRecord)); String id = "someId"; BackupInfoRecord actualBiRecord = backupService.getBackup(USER, id); assertNotNull(actualBiRecord); assertEquals(biRecord, actualBiRecord); verify(backupDao).getBackup(USER, id); verifyNoMoreInteractions(backupDao); }
@Test public void getBackupWithException() { String id = "someId"; doThrow(new ResourceNotFoundException(String.format("Backup with id %s was not found for user %s", id, USER))) .when(backupDao).getBackup(USER, id); expectedException.expect(ResourceNotFoundException.class); expectedException.expectMessage("Backup with id " + id + " was not found for user " + USER); backupService.getBackup(USER, id); }
@Test public void getBackupWhenBackupIsAbsent() { String id = "someId"; when(backupDao.getBackup(USER, id)).thenReturn(Optional.empty()); expectedException.expect(ResourceNotFoundException.class); expectedException.expectMessage("Backup with id " + id + " was not found for user " + USER); backupService.getBackup(USER, id); }