TodoController { @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public TodoDTO update(@PathVariable("id") Long id, @RequestBody @Valid TodoDTO dto) { dto.setId(id); LOGGER.info("Updating todo entry with information: {}", dto); TodoDTO updated = crudService.update(dto); LOGGER.info("Updated todo entry: {}", updated); return updated; } @Autowired  TodoController(TodoCrudService crudService, TodoSearchService searchService); @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) TodoDTO add(@RequestBody @Valid TodoDTO dto); @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) TodoDTO delete(@PathVariable("id") Long id); @RequestMapping(method = RequestMethod.GET) List<TodoDTO> findAll(); @RequestMapping(value = "/{id}", method = RequestMethod.GET) TodoDTO findById(@PathVariable("id") Long id); @RequestMapping(value = "/search", method = RequestMethod.GET) Page<TodoDTO> findBySearchTerm(@RequestParam("searchTerm") String searchTerm, Pageable pageable); @RequestMapping(value = "/{id}", method = RequestMethod.PUT) TodoDTO update(@PathVariable("id") Long id, @RequestBody @Valid TodoDTO dto);  }
@Test public void update_TodoEntryNotFound_ShouldReturnHttpStatusCodeNotFound() throws Exception { TodoDTO updatedTodoEntry = new TodoDTOBuilder() .description(DESCRIPTION) .title(TITLE) .build(); when(todoCrudServiceMock.update(isA(TodoDTO.class))).thenThrow(new TodoNotFoundException("")); mockMvc.perform(put("/api/todo/{id}", ID) .contentType(WebTestConstants.APPLICATION_JSON_UTF8) .content(objectMapper.writeValueAsBytes(updatedTodoEntry)) ) .andExpect(status().isNotFound()); ArgumentCaptor<TodoDTO> serviceMethodArgument = ArgumentCaptor.forClass(TodoDTO.class); verify(todoCrudServiceMock, times(1)).update(serviceMethodArgument.capture()); verifyNoMoreInteractions(todoCrudServiceMock); verifyZeroInteractions(todoSearchServiceMock); TodoDTO serviceMethodArgumentValue = serviceMethodArgument.getValue(); assertThatTodoDTO(serviceMethodArgumentValue) .hasId(ID) .hasNoCreationTime() .hasDescription(DESCRIPTION) .hasNoModificationTime() .hasTitle(TITLE); }
@Test public void update_TodoEntryFound_ShouldReturnUpdatedTodoEntryAsJsonDocument() throws Exception { TodoDTO updatedTodoEntry = new TodoDTOBuilder() .description(DESCRIPTION) .title(TITLE) .build(); TodoDTO returnedTodoEntry = new TodoDTOBuilder() .creationTime(CREATION_TIME) .description(DESCRIPTION) .id(ID) .modificationTime(MODIFICATION_TIME) .title(TITLE) .build(); when(todoCrudServiceMock.update(isA(TodoDTO.class))).thenReturn(returnedTodoEntry); mockMvc.perform(put("/api/todo/{id}", ID) .contentType(WebTestConstants.APPLICATION_JSON_UTF8) .content(objectMapper.writeValueAsBytes(updatedTodoEntry)) ) .andExpect(status().isOk()) .andExpect(content().contentType(WebTestConstants.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$.id", is(ID.intValue()))) .andExpect(jsonPath("$.creationTime", is(CREATION_TIME))) .andExpect(jsonPath("$.description", is(DESCRIPTION))) .andExpect(jsonPath("$.modificationTime", is(MODIFICATION_TIME))) .andExpect(jsonPath("$.title", is(TITLE))); ArgumentCaptor<TodoDTO> serviceMethodArgument = ArgumentCaptor.forClass(TodoDTO.class); verify(todoCrudServiceMock, times(1)).update(serviceMethodArgument.capture()); verifyNoMoreInteractions(todoCrudServiceMock); verifyZeroInteractions(todoSearchServiceMock); TodoDTO serviceMethodArgumentValue = serviceMethodArgument.getValue(); assertThatTodoDTO(serviceMethodArgumentValue) .hasId(ID) .hasNoCreationTime() .hasDescription(DESCRIPTION) .hasNoModificationTime() .hasTitle(TITLE); }