@Test public void non_Null_User() { // Mock CustomerSpecialsService customerSpecialsService = mock(CustomerSpecialsService.class); WeeklySpecialsService weeklySpecialsService = new WeeklySpecialsService(); // Stubbing when(mockedBookDao.getTop5BooksOnSale()).thenReturn(top5BooksOnSaleList); when(mockedBookDao.getSpecialPromotionsBasedOnUser(user)) .thenReturn(booksOnSpecialPromotionList); // Look below is // when(customerSpecialsService.applySpecials(anyList(), // Matchers.<User>anyObject())).thenReturn(booksOnSpecialPromotionList); final PromotionsService promotionsService = new PromotionsService(); promotionsService.setBookDao(mockedBookDao); // Look mocked DAO promotionsService.setCustomerSpecialsService(customerSpecialsService); // This is mocked too! promotionsService.setWeeklySpecialsService( weeklySpecialsService); // And this one too is mocked! // Passing in null user final List<Book> promotionList = promotionsService.getPromotions(user); verify(mockedBookDao, never()).getTop5BooksOnSale(); verify(mockedBookDao, times(1)).getSpecialPromotionsBasedOnUser(user); assertNotNull(promotionList); System.out.println("Size" + promotionList.size()); assertTrue(promotionList.size() == 3); }
@Test public void services_Are_Being_Mocked_Here() { // Mock CustomerSpecialsService customerSpecialsService = mock(CustomerSpecialsService.class); WeeklySpecialsService weeklySpecialsService = mock(WeeklySpecialsService.class); // Stubbing when(mockedBookDao.getTop5BooksOnSale()).thenReturn(top5BooksOnSaleList); when(mockedBookDao.getSpecialPromotionsBasedOnUser(null)) .thenReturn(booksOnSpecialPromotionList); when(customerSpecialsService.getSpecials()).thenReturn(booksOnSpecialPromotionList); final PromotionsService promotionsService = new PromotionsService(); promotionsService.setBookDao(mockedBookDao); // Inject mocked DAO promotionsService.setCustomerSpecialsService(customerSpecialsService); // This is mocked too! promotionsService.setWeeklySpecialsService( weeklySpecialsService); // And this one too is mocked! // Passing in null user final List<Book> promotionList = promotionsService.getPromotions(null); verify(mockedBookDao).getTop5BooksOnSale(); verify(mockedBookDao, never()).getSpecialPromotionsBasedOnUser(null); verify(customerSpecialsService, never()).applySpecials(anyList(), Matchers.<User>anyObject()); verify(customerSpecialsService).getSpecials(); assertNotNull(promotionList); assertTrue(promotionList.size() == 3); }