TodoController { @RequestMapping(method = RequestMethod.GET) public List<TodoDTO> findAll() { LOGGER.info("Finding all todo entries"); List<TodoDTO> todoEntries = crudService.findAll(); LOGGER.info("Found {} todo entries."); return todoEntries; } @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 findAll_NoTodoEntriesFound_ShouldReturnEmptyListAsJSonDocument() throws Exception { when(todoCrudServiceMock.findAll()).thenReturn(new ArrayList<TodoDTO>()); mockMvc.perform(get("/api/todo")) .andExpect(status().isOk()) .andExpect(content().contentType(WebTestConstants.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$", hasSize(0))); verify(todoCrudServiceMock, times(1)).findAll(); verifyNoMoreInteractions(todoCrudServiceMock); verifyZeroInteractions(todoSearchServiceMock); }
@Test public void findAll_OneTodoEntryFound_ShouldReturnTodoEntriesAsJsonDocument() throws Exception { TodoDTO foundTodoEntry = new TodoDTOBuilder() .creationTime(CREATION_TIME) .description(DESCRIPTION) .id(ID) .modificationTime(MODIFICATION_TIME) .title(TITLE) .build(); when(todoCrudServiceMock.findAll()).thenReturn(Arrays.asList(foundTodoEntry)); mockMvc.perform(get("/api/todo")) .andExpect(status().isOk()) .andExpect(content().contentType(WebTestConstants.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$[0].id", is(ID.intValue()))) .andExpect(jsonPath("$[0].creationTime", is(CREATION_TIME))) .andExpect(jsonPath("$[0].description", is(DESCRIPTION))) .andExpect(jsonPath("$[0].modificationTime", is(MODIFICATION_TIME))) .andExpect(jsonPath("$[0].title", is(TITLE))); verify(todoCrudServiceMock, times(1)).findAll(); verifyNoMoreInteractions(todoCrudServiceMock); verifyZeroInteractions(todoSearchServiceMock); }