@Test public void testGetTopic() throws NotFoundException { Topic expectedTopic = new Topic(user, "title"); when(topicDao.isExist(999L)).thenReturn(true); when(topicDao.get(999L)).thenReturn(expectedTopic); int viewsCount = expectedTopic.getViews(); Topic actualTopic = topicFetchService.get(999L); assertEquals(actualTopic.getViews(), viewsCount + 1); assertEquals(actualTopic, expectedTopic, "Topics aren't equal"); verify(topicDao).isExist(999L); verify(topicDao).get(999L); }
/** * Deactivates branch updates subscription for the current user * * @param id identifies topic to unsubscribe from * @throws NotFoundException if no object is found for id given */ @RequestMapping("topics/{id}/unsubscribe") @ResponseBody public ModelAndView unsubscribeFromTopic( @PathVariable Long id, @RequestHeader(value = "X-Requested-With", defaultValue = "NotAjax") String header) throws NotFoundException { Topic topic = topicFetchService.get(id); subscriptionService.toggleTopicSubscription(topic); if (header.equals("NotAjax")) { // redirect to new page return new ModelAndView("redirect:/topics/" + id); } else { // no redirecting, perform AJAX request return null; } }
@Test(expectedExceptions = {NotFoundException.class}) public void testGetTopicWithIncorrectId() throws NotFoundException { when(topicDao.isExist(333L)).thenReturn(false); topicFetchService.get(333L); }