3738394041424344454647
public void fillLibrary() { if (getNumBooks() != 0) { return; } Author mann = createAuthor("Thomas", "Mann"); Author steinbeck = createAuthor("John", "Steinbeck"); createBook("Buddenbrooks", mann); createBook("East of Eden", steinbeck); }
71727374757677787980818283
return books; } public Author createAuthor(String firstName, String lastName) { em.getTransaction().begin(); Author author = new Author(); author.setFirstName(firstName); author.setLastName(lastName); em.persist(author); em.flush(); em.getTransaction().commit(); return author; }
4142434445464748
public void byTitle() { service.fillLibrary(); List<Book> books = service.findBooksByTitle("East of Eden"); assertEquals(1, books.size()); Book book = books.get(0); assertEquals("Steinbeck", book.getAuthor().getLastName()); }
4748495051525354
@Test public void byAuthor() { List<Book> books = service.findBooksByAuthor("Mann"); assertEquals(1, books.size()); Book book = books.get(0); assertEquals("Buddenbrooks", book.getTitle()); }
6768697071727374
@Test public void byAuthor() { List<Book> books = service.findBooksByAuthor(authorLastName); assertEquals(1, books.size()); Book book = books.get(0); assertEquals(bookTitle, book.getTitle()); }
82838485868788899091929394
return author; } public Book createBook(String title, Author author) { em.getTransaction().begin(); Book book = new Book(); book.setTitle(title); book.setAuthor(author); author.getBooks().add(book); em.persist(book); em.flush(); em.getTransaction().commit(); return book;
54555657585960
return bean.getObject(); } @Bean public LibraryService libraryService() { return new LibraryService(); }
53545556575859