Ejemplo n.º 1
0
  @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.
  }
Ejemplo n.º 2
0
  @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);
  }
Ejemplo n.º 3
0
  @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);
  }
Ejemplo n.º 4
0
  @Test
  public void last_Stubbing_Rules() {
    Book book1 = new Book().setTitle("Book #1");
    Book book2 = new Book().setTitle("Book #2");

    when(mockedBookDao.get(1L)).thenReturn(book1);
    when(mockedBookDao.get(1L)).thenReturn(book2);

    assertEquals("Book #2", mockedBookDao.get(1L).getTitle());
  }
Ejemplo n.º 5
0
  @Test
  public void show_Multiple_Stubbing_1() {
    Book book1 = new Book().setTitle("Book #1");
    Book book2 = new Book().setTitle("Book #2");

    when(mockedBookDao.get(1L)).thenReturn(book1).thenReturn(book2);
    assertEquals("Book #1", mockedBookDao.get(1L).getTitle());

    when(mockedBookDao.get(1L)).thenReturn(book2);
    assertEquals("Book #2", mockedBookDao.get(1L).getTitle());
  }
Ejemplo n.º 6
0
  @Test(expected = IllegalStateException.class)
  public void stub_Exceptions_2() {

    doThrow(new IllegalStateException("Illegal")).when(mockedBookDao).remove(1L);
    mockedBookDao.remove(1L);
  }
Ejemplo n.º 7
0
  @Test(expected = RuntimeException.class)
  public void stub_Exceptions_1() {

    when(mockedBookDao.get(1L)).thenThrow(new RuntimeException());
    mockedBookDao.get(1L);
  }