### Question:
TopicSearchController { @RequestMapping(value = "/search", method = RequestMethod.GET) public ModelAndView initSearch(@RequestParam(value = "text", defaultValue = "", required = false) String searchText, @RequestParam(value = "page", defaultValue = "1", required = false) String page) { Page<Topic> searchResultPage = topicSearchService.searchByTitleAndContent(searchText, page); lastReadPostService.fillLastReadPostForTopics(searchResultPage.getContent()); HashMap<String, Object> urlParams = new HashMap<>(); urlParams.put("text", searchText); return new ModelAndView(SEARCH_RESULT_VIEW_NAME). addObject(SEARCH_RESULT_ATTRIBUTE_NAME, converter.convertTopicPageToTopicDtoPage(searchResultPage)). addObject(SEARCH_TEXT_ATTRIBUTE_NAME, searchText). addObject("urlParams", urlParams); } @Autowired  TopicSearchController(TopicFetchService topicSearchService,
                                 LastReadPostService lastReadPostService,
                                 EntityToDtoConverter converter); @RequestMapping(value = "/search/index/rebuild") @ResponseStatus(value = HttpStatus.NO_CONTENT) void rebuildIndexes(); @RequestMapping(value = "/search", method = RequestMethod.GET) ModelAndView initSearch(@RequestParam(value = "text", defaultValue = "", required = false) String searchText,
                                   @RequestParam(value = "page", defaultValue = "1", required = false) String page); static final String URI_ATTRIBUTE_NAME; static final String SEARCH_RESULT_ATTRIBUTE_NAME; static final String SEARCH_TEXT_ATTRIBUTE_NAME; }### Answer:
@Test public void testInitSearch() { Page<Topic> searchResultPage = new PageImpl<>(Collections.<Topic> emptyList()); Page<TopicDto> searchDtoResultPage = new PageImpl<>(Collections.<TopicDto> emptyList()); when(topicFetchService.searchByTitleAndContent(DEFAULT_SEARCH_TEXT, START_PAGE)) .thenReturn(searchResultPage); when(converter.convertTopicPageToTopicDtoPage(searchResultPage)).thenReturn(searchDtoResultPage); ModelAndView modelAndView = topicSearchController.initSearch(DEFAULT_SEARCH_TEXT, "1"); Map<String, Object> model = modelAndView.getModel(); Assert.assertEquals(searchDtoResultPage, model.get(TopicSearchController.SEARCH_RESULT_ATTRIBUTE_NAME), "The controller must return the result of TopicFullSearchService."); verify(topicFetchService).searchByTitleAndContent(DEFAULT_SEARCH_TEXT, START_PAGE); verify(lastReadPostService).fillLastReadPostForTopics(searchResultPage.getContent()); }
@Test public void testContinueSearch() { Page<Topic> searchResultPage = new PageImpl<>(Collections.<Topic> emptyList()); Page<TopicDto> searchDtoResultPage = new PageImpl<>(Collections.<TopicDto> emptyList()); String page = "2"; when(topicFetchService.searchByTitleAndContent(DEFAULT_SEARCH_TEXT, page)) .thenReturn(searchResultPage); when(converter.convertTopicPageToTopicDtoPage(searchResultPage)).thenReturn(searchDtoResultPage); ModelAndView modelAndView = topicSearchController.initSearch(DEFAULT_SEARCH_TEXT, page); Map<String, Object> model = modelAndView.getModel(); Assert.assertEquals(searchDtoResultPage, model.get(TopicSearchController.SEARCH_RESULT_ATTRIBUTE_NAME), "The controller must return the result of TopicFullSearchService."); verify(topicFetchService).searchByTitleAndContent(DEFAULT_SEARCH_TEXT, page); verify(lastReadPostService).fillLastReadPostForTopics(searchResultPage.getContent()); }