ProjectService { @Transactional public void createProject(String name) { if (projectRepository.existsById(name)) { throw new IllegalArgumentException(String.format("Project already exists: %s", name)); } Project project = new Project(name); projectRepository.saveAndFlush(project); } @Autowired  ProjectService(ProjectRepository projectRepository); @Transactional void createProject(String name); @Transactional void archiveProject(String name); @Transactional List<Project> listProjects();  }
@Test public void shouldCreateProjectIfItDoesntExist() { String projectName = "project1"; Project project = new Project(projectName); when(projectRepository.saveAndFlush(project)).thenReturn(project); projectService.createProject(projectName); verify(projectRepository, times(1)).saveAndFlush(project); }
@Test(expected = IllegalArgumentException.class) public void shouldNotCreateProjectIfItExist() { String projectName = "project1"; when(projectRepository.existsById(projectName)).thenReturn(true); projectService.createProject(projectName); }