### Question:
SubscriptionController { @RequestMapping("branches/{id}/subscribe") @ResponseBody public ModelAndView subscribeToBranch(@PathVariable Long id, @RequestHeader(value = "X-Requested-With", defaultValue = "NotAjax") String header) throws NotFoundException { Branch branch = branchService.get(id); subscriptionService.toggleBranchSubscription(branch); if (header.equals("NotAjax")) { return new ModelAndView("redirect:/branches/" + id); } else { return null; } } @Autowired  SubscriptionController(TopicFetchService topicFetchService,
                                  BranchService branchService,
                                  SubscriptionService subscriptionService); @RequestMapping("topics/{id}/subscribe") @ResponseBody ModelAndView subscribeToTopic(@PathVariable Long id, @RequestHeader(value = "X-Requested-With", defaultValue = "NotAjax") String header); @RequestMapping("topics/{id}/unsubscribe") @ResponseBody ModelAndView unsubscribeFromTopic(@PathVariable Long id, @RequestHeader(value = "X-Requested-With", defaultValue = "NotAjax") String header
    ); @RequestMapping("branches/{id}/subscribe") @ResponseBody ModelAndView subscribeToBranch(@PathVariable Long id, @RequestHeader(value = "X-Requested-With", defaultValue = "NotAjax") String header); @RequestMapping("branches/{id}/unsubscribe") @ResponseBody ModelAndView unsubscribeFromBranch(@PathVariable Long id, @RequestHeader(value = "X-Requested-With", defaultValue = "NotAjax") String header);  }### Answer:
@Test public void testSubscribeOnBranch() throws NotFoundException { when(branchService.get(id)).thenReturn(branch); ModelAndView actualMav = controller.subscribeToBranch(id, "NotAjax"); verify(subscriptionService).toggleBranchSubscription(branch); assertViewName(actualMav, "redirect:/branches/" + id); }
@Test public void testSubscribeOnBranchReturnNull() throws NotFoundException { when(branchService.get(id)).thenReturn(branch); ModelAndView actualMav = controller.subscribeToBranch(id, anyString()); verify(subscriptionService).toggleBranchSubscription(branch); Assert.assertEquals(actualMav, null); }
@Test(expectedExceptions = NotFoundException.class) public void testSubscribeOnNonexistingBranch() throws NotFoundException { doThrow(new NotFoundException()).when(branchService).get(id); controller.subscribeToBranch(id, anyString()); }