@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 show_How_Stubbing_Works() { /** * Set up phase. * * <p>1. Create Mocks, 2. Stub only required methods 3. Inject the mock objects. */ // Stubbing when(mockedBookDao.getTop5BooksOnSale()).thenReturn(top5BooksOnSaleList); when(mockedBookDao.getSpecialPromotionsBasedOnUser(Matchers.<User>anyObject())) .thenReturn(booksOnSpecialPromotionList); // Injecting mocked DAO promotionsService.setBookDao(mockedBookDao); // Calling business method final List<Book> promotionList = promotionsService.getSimplePromotions(user); // Verification of behavior verify(mockedBookDao).getSpecialPromotionsBasedOnUser(user); verify(mockedBookDao, never()).getTop5BooksOnSale(); assertNotNull(promotionList); assertTrue( promotionList.size() == 3); // Stubbed method being called because the size is 3. The real method returns 1 // element list. }
@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); }