RepositoryTodoCrudService implements TodoCrudService { @Transactional @Override public TodoDTO update(TodoDTO dto) { LOGGER.info("Updating the information of a todo entry: {}", dto); Todo newInformation = createModel(dto); Todo updated = repository.update(newInformation); LOGGER.debug("Updated the information of a todo entry: {}", updated); return transformer.convert(updated, new TodoDTO()); } @Autowired  RepositoryTodoCrudService(TodoRepository repository, JTransfo transformer); @Transactional @Override TodoDTO add(TodoDTO dto); @Transactional @Override TodoDTO delete(Long id); @Transactional(readOnly = true) @Override List<TodoDTO> findAll(); @Transactional(readOnly = true) @Override TodoDTO findById(Long id); @Transactional @Override TodoDTO update(TodoDTO dto);  }
@Test public void update_TodoEntryNotFound_ShouldThrowException() { TodoDTO updatedTodoEntry = new TodoDTOBuilder() .id(ID) .description(DESCRIPTION) .title(TITLE) .build(); when(repositoryMock.update(isA(Todo.class))).thenThrow(new TodoNotFoundException("")); catchException(service).update(updatedTodoEntry); assertThat(caughtException()).isExactlyInstanceOf(TodoNotFoundException.class); ArgumentCaptor<Todo> repositoryMethodArgument = ArgumentCaptor.forClass(Todo.class); verify(repositoryMock, times(1)).update(repositoryMethodArgument.capture()); verifyNoMoreInteractions(repositoryMock); Todo repositoryMethodArgumentValue = repositoryMethodArgument.getValue(); assertThatTodo(repositoryMethodArgumentValue) .hasDescription(DESCRIPTION) .hasId(ID) .hasTitle(TITLE) .creationTimeIsNotSet() .modificationTimeIsNotSet(); }
@Test public void update_TodoEntryFound_ShouldUpdateTodoEntryAndReturnUpdatedTodoEntry() { TodoDTO existingTodoEntry = new TodoDTOBuilder() .id(ID) .description(DESCRIPTION) .title(TITLE) .build(); Todo updatedTodoEntry = Todo.getBuilder(TITLE) .creationTime(CREATION_TIME) .description(DESCRIPTION) .id(ID) .modificationTime(MODIFICATION_TIME) .build(); when(repositoryMock.update(isA(Todo.class))).thenReturn(updatedTodoEntry); TodoDTO returnedTodoEntry = service.update(existingTodoEntry); ArgumentCaptor<Todo> repositoryMethodArgument = ArgumentCaptor.forClass(Todo.class); verify(repositoryMock, times(1)).update(repositoryMethodArgument.capture()); verifyNoMoreInteractions(repositoryMock); Todo repositoryMethodArgumentValue = repositoryMethodArgument.getValue(); assertThatTodo(repositoryMethodArgumentValue) .hasDescription(DESCRIPTION) .hasId(ID) .hasTitle(TITLE) .creationTimeIsNotSet() .modificationTimeIsNotSet(); assertThatTodoDTO(returnedTodoEntry) .hasDescription(DESCRIPTION) .hasId(ID) .hasTitle(TITLE) .wasCreatedAt(CREATION_TIME_STRING) .wasModifiedAt(MODIFICATION_TIME_STRING); }