ProjectService { @Transactional public void archiveProject(String name) { Optional<Project> project = projectRepository.findById(name); if (!project.isPresent()) { throw new IllegalArgumentException(String.format("Could not find project: \"%s\"", name)); } if (name.equals(Project.DEFAULT_NAME)) { throw new UnsupportedOperationException("Archiving the default project is not allowed."); } Project p = project.get(); p.setArchived(true); projectRepository.saveAndFlush(p); } @Autowired  ProjectService(ProjectRepository projectRepository); @Transactional void createProject(String name); @Transactional void archiveProject(String name); @Transactional List<Project> listProjects();  }
@Test public void shouldArchiveProjectIfItExists() { String projectName = "project1"; Project project = new Project(projectName); when(projectRepository.findById(projectName)).thenReturn(Optional.of(project)); projectService.archiveProject(projectName); verify(projectRepository, times(1)).saveAndFlush(project); }
@Test public void shouldNotArchiveDefaultProject() { expectedException.expect(IllegalArgumentException.class); this.projectService.archiveProject(Project.DEFAULT_NAME); }
@Test(expected = IllegalArgumentException.class) public void shouldNotArchiveProjectIfItIsAlreadyArchived() { String projectName = "project1"; when(projectRepository.findById(projectName)).thenReturn(Optional.empty()); projectService.archiveProject(projectName); }