@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); }
@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); }
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); } }
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); }
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); }
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);  }