CounterDAO { public void delete(User user, Long projectId, List<Long> counterIds) throws NotFoundException { final Project project = projectRepository.findById(projectId).orElse(null); final List<Counter> counters = counterRepository.findAllByIdIn(counterIds); if (counters.size() != counterIds.size()) { throw new NotFoundException("At least one counter cannot be found"); } for (Counter counter : counters) { checkAccess(user, project, counter); } counterRepository.deleteAll(counters); } @Autowired  CounterDAO(ProjectDAO projectDAO,
                      CounterRepository counterRepository,
                      ProjectRepository projectRepository); void create(User user, Counter counter); List<Counter> getAll(User user, Long projectId); Counter update(User user, Counter counter); void delete(User user, Long projectId, List<Long> counterIds); void checkAccess(User user, Project project, Counter counter);  }
@Test(expected = NotFoundException.class) public void shouldFailToDeleteACounterThatDoesNotExist() throws NotFoundException { User user = new User(); counterDAO.delete(user, PROJECT_ID, Collections.singletonList(-1L)); }
@Test public void shouldDeleteACounter() throws NotFoundException { final Long counterId = 1L; User user = new User(); user.setId(USER_ID); Project project = new Project(); project.setId(PROJECT_ID); Counter counter = new Counter(); counter.setId(counterId); counter.setProject(project); List<Counter> counterAsList = Collections.singletonList(counter); given(projectRepository.findById(PROJECT_ID)).willReturn(Optional.of(project)); given(counterRepository.findAllByIdIn(Collections.singletonList(counterId))).willReturn(counterAsList); counterDAO.delete(user, PROJECT_ID, Collections.singletonList(counterId)); verify(counterRepository).deleteAll(counterAsList); }