### Question:
ProfileController { @RequestMapping(value = {"/{username:.*}"}, method = RequestMethod.GET) public String viewProfileByUsername(@PathVariable String username, ModelMap model, @RequestParam(required = false) String referringPageId, HttpServletResponse response) { User user = null; try{ user = userService.getUserByUsername(username); logger.debug("Viewing person profile for: " + user.getUsername()); return viewProfileCommon(user, model, referringPageId); }catch(Exception e){ return profileNotFoundErrorHelper(model, referringPageId, response, user, e); } } @Autowired  ProfileController(UserService userService, PageService pageService); @RequestMapping(value = {"/{username:.*}"}, method = RequestMethod.GET) String viewProfileByUsername(@PathVariable String username, ModelMap model, @RequestParam(required = false) String referringPageId, HttpServletResponse response); @RequestMapping(value = {"/id/{userid:.*}"}, method = RequestMethod.GET) String viewProfile(@PathVariable String userid, ModelMap model, @RequestParam(required = false) String referringPageId, HttpServletResponse response); @RequestMapping(method = RequestMethod.POST) String updateProfile(ModelMap model,
                                @RequestParam(required = false) String referringPageId,
                                @ModelAttribute("updatedUser") UserForm updatedUser); static void addNavItemsToModel(String view, ModelMap model, String referringPageId, User user, List<Person> friendRequests);  }### Answer:
@Test public void viewPerson_ShouldAddAttributeForUser() { final UserImpl user = new UserImpl(); final ModelMap model = new ModelMap(); final int modelSize = 4; final String username="canonical"; user.setUsername(username); user.setId(USER_ID); String userProfile = new String(ModelKeys.USER_PROFILE); Page personProfile = new PageImpl(); PageLayout pageLayout = new PageLayoutImpl(); pageLayout.setCode(VALID_PAGE_LAYOUT_CODE); personProfile.setPageLayout(pageLayout); List<Person> personObjects = new ArrayList<Person>(); expect(userService.getUserByUsername(username)).andReturn(user).once(); expect(pageService.getPersonProfilePage(user.getId())).andReturn(personProfile); expect(userService.getFriendRequestsReceived(username)).andReturn(personObjects); replay(userService, pageService); String view = profileController.viewProfileByUsername(username, model, null, response); assertThat(model, CoreMatchers.notNullValue()); assertThat(model.size(), CoreMatchers.equalTo(modelSize)); assertThat(model.containsAttribute(userProfile), CoreMatchers.equalTo(true)); assertThat(model.get(userProfile), CoreMatchers.notNullValue()); assertThat(view, is(ViewNames.PERSON_PROFILE + "." + VALID_PAGE_LAYOUT_CODE)); verify(userService, pageService); }
@Test public void viewPersonProfile_invalidUser() { final User user = null; final ModelMap model = new ModelMap(); final int modelSize = 4; final String username="Canonical"; Page personProfile = new PageImpl(); PageLayout pageLayout = new PageLayoutImpl(); pageLayout.setCode("person_profile"); personProfile.setPageLayout(pageLayout); expect(userService.getUserByUsername(username)).andThrow(new UsernameNotFoundException("Username does not exist")); replay(userService, pageService); String view = profileController.viewProfileByUsername(username, model, null, response); assertThat(view, is(ViewNames.USER_NOT_FOUND)); assertThat(response.getStatus(), is(HttpStatus.NOT_FOUND.value())); verify(userService, pageService); }