### Question:
PageController { @RequestMapping(value = {"/page/view", "/index.html"}, method = RequestMethod.GET) public String viewDefault(Model model, HttpServletRequest request) { List<Page> pages = getAllPagesForAuthenticatedUser(); Page page = pageService.getDefaultPageFromList(pages); PageUser currentPageUser = null; User thisUser = userService.getAuthenticatedUser(); for(PageUser pageUser : page.getMembers()){ if(pageUser.getUserId().equals(thisUser.getId())){ currentPageUser = pageUser; } } List<PageLayout> pageLayouts = pageLayoutService.getAllUserSelectable(); addAttributesToModel(model, page, currentPageUser, pages, pageLayouts); String view = ControllerUtils.getDeviceAppropriateView(request, ViewNames.getPageView(page.getPageLayout().getCode()), ViewNames.MOBILE_HOME); ControllerUtils.addNavItemsToModel(view, model, page.getId(), userService.getAuthenticatedUser(), currentPageUser.isEditor()); return view; } @Autowired  PageController(PageService pageService, UserService userService, PageLayoutService pageLayoutService); @RequestMapping(value = {"/login"}, method = RequestMethod.GET) String login(Model model, HttpServletRequest httpServletRequest); @RequestMapping(value = {"/page/view", "/index.html"}, method = RequestMethod.GET) String viewDefault(Model model, HttpServletRequest request); @RequestMapping(value = "/page/view/{pageId}", method = RequestMethod.GET) String view(@PathVariable String pageId, Model model, HttpServletRequest request);  }### Answer:
@SuppressWarnings("unchecked") @Test public void viewDefault_pageId() { MockHttpUtil.setupRequestAsNonMobileUserAgent(request); expect(userService.getAuthenticatedUser()).andReturn(validUser).anyTimes(); expect(pageService.getAllUserPages(USER_ID)).andReturn(allPages); expect(pageService.getDefaultPageFromList(allPages)).andReturn(defaultPage); expect(pageLayoutService.getAllUserSelectable()).andReturn(allPageLayouts); replay(userService, pageService, pageLayoutService); String results = pageController.viewDefault(model, request); assertThat(results, equalTo(ViewNames.getPageView(VALID_PAGE_LAYOUT_CODE))); assertThat((Page) model.asMap().get(ModelKeys.PAGE), sameInstance(defaultPage)); assertThat((List<Page>) model.asMap().get(ModelKeys.PAGES), equalTo(allPages)); assertThat((List<PageLayout>) model.asMap().get(ModelKeys.PAGE_LAYOUTS), sameInstance(allPageLayouts)); verify(userService, pageService, pageLayoutService); }
@SuppressWarnings("unchecked") @Test public void viewDefault_pageId_zeroExistingPages() { MockHttpUtil.setupRequestAsNonMobileUserAgent(request); List<Page> pages = new ArrayList<Page>(); assertThat(pages.isEmpty(), is(true)); expect(userService.getAuthenticatedUser()).andReturn(validUser).anyTimes(); expect(pageService.getAllUserPages(USER_ID)).andReturn(pages).times(2); expect(pageService.addNewDefaultUserPage(validUser.getId())).andReturn(defaultPage); expect(pageService.getDefaultPageFromList(pages)).andReturn(defaultPage); expect(pageLayoutService.getAllUserSelectable()).andReturn(allPageLayouts); replay(userService, pageService, pageLayoutService); String results = pageController.viewDefault(model, request); assertThat(results, equalTo(ViewNames.getPageView(VALID_PAGE_LAYOUT_CODE))); assertThat((Page) model.asMap().get(ModelKeys.PAGE), sameInstance(defaultPage)); assertThat((List<Page>) model.asMap().get(ModelKeys.PAGES), sameInstance(pages)); assertThat((List<PageLayout>) model.asMap().get(ModelKeys.PAGE_LAYOUTS), sameInstance(allPageLayouts)); verify(userService, pageService, pageLayoutService); }