@Test public void findAllTest() throws Exception { Book book1 = BookFactory.createBook(); Book book2 = BookFactory.createBook(); List<Book> bookList = Arrays.asList(book1, book2); when(this.bookDAOMock.findAll()).thenReturn(bookList); List<Book> result = this.bookService.findAll(); assertEquals(bookList, result); }
@Test public void deleteTest() throws Exception { Book book = BookFactory.createBook(); this.bookService.delete(book); verify(this.bookDAOMock, times(1)).delete(book); }
@Test public void findOneTest() throws Exception { Book book = BookFactory.createBook(); when(this.bookDAOMock.findOne(anyString())).thenReturn(book); Book result = this.bookService.findOne("123456789"); assertEquals(book, result); }
@Test public void updateTest() throws Exception { Book book = BookFactory.createBook(); when(this.bookDAOMock.update(book)).thenReturn(book); Book result = this.bookService.update(book); assertEquals(book, result); }
@Test(expected = GestionAppException.class) public void decreaseStockIlegalStockTest() throws GestionAppException, DataAccessException { Book book = BookFactory.createBook(); book.setCurrentStock(10); when(this.bookDAOMock.findOne(anyString())).thenReturn(book); this.bookService.decreaseStock(book.getIsbn(), 15); }
@Test public void saveTest() throws Exception { Book book = BookFactory.createBook(); String expectedId = book.getIsbn(); when(this.bookDAOMock.save(book)).thenReturn(expectedId); String result = this.bookService.save(book); assertEquals(expectedId, result); }
@Test(expected = GestionAppException.class) public void decreaseStockCannotUpdateBookTest() throws GestionAppException, DataAccessException { Book book = BookFactory.createBook(); String isbn = book.getIsbn(); book.setCurrentStock(10); when(this.bookDAOMock.findOne(isbn)).thenReturn(book); when(this.bookDAOMock.update(book)).thenThrow(DataAccessException.class); this.bookService.decreaseStock(isbn, 2); }
@Test public void decreaseStockTest() throws GestionAppException, DataAccessException { Book book = BookFactory.createBook(); String isbn = book.getIsbn(); book.setCurrentStock(10); when(this.bookDAOMock.findOne(isbn)).thenReturn(book); when(this.bookDAOMock.update(book)).thenReturn(book); this.bookService.decreaseStock(isbn, 2); verify(this.bookDAOMock, times(1)).findOne(isbn); verify(this.bookDAOMock, times(1)).update(book); assertEquals(8, book.getCurrentStock()); }