### Question:
BookServiceImpl implements BookService { @Override @Transactional public Book saveBook(@NotNull @Valid final Book book) { LOGGER.debug("Creating {}", book); Book existing = repository.findOne(book.getId()); if (existing != null) { throw new BookAlreadyExistsException( String.format("There already exists a book with id=%s", book.getId())); } return repository.save(book); } @Autowired  BookServiceImpl(final BookRepository repository); @Override @Transactional Book saveBook(@NotNull @Valid final Book book); @Override @Transactional(readOnly = true) List<Book> getList(); @Override Book getBook(Long bookId); @Override @Transactional void deleteBook(final Long bookId);  }### Answer:
@Test public void shouldSaveNewUser_GivenThereDoesNotExistOneWithTheSameId_ThenTheSavedUserShouldBeReturned() throws Exception { final Book savedBook = stubRepositoryToReturnUserOnSave(); final Book book = UserUtil.createBook(); final Book returnedBook = bookService.saveBook(book); verify(bookRepository, times(1)).save(book); assertEquals("Returned book should come from the repository", savedBook, returnedBook); }
@Test public void shouldSaveNewUser_GivenThereExistsOneWithTheSameId_ThenTheExceptionShouldBeThrown() throws Exception { stubRepositoryToReturnExistingUser(); try { bookService.saveBook(UserUtil.createBook()); fail("Expected exception"); } catch (BookAlreadyExistsException ignored) { } verify(bookRepository, never()).save(any(Book.class)); }