12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 7 ■ SESSION BEANS AND THE TIMER SERVICEListing 7-19. A Test Class Using the Embeddable Containerpublic class ItemEJBTest {private static EJBContainer ec;private static Context ctx;@BeforeClasspublic static void initContainer() throws Exception {ec = EJBContainer.createEJBContainer();ctx = ec.getContext();}@AfterClasspublic static void closeContainer() throws Exception {if (ec != null)ec.close();}@Testpublic void shouldCreateABook() throws Exception {// Creates an instance of bookBook book = new Book();book.setTitle("The Hitchhiker's Guide to the Galaxy");book.setPrice(12.5F);book.setDescription("Science fiction comedy book");book.setIsbn("1-84023-742-2");book.setNbOfPage(354);book.setIllustrations(false);// Looks up for the EJBItemEJB bookEJB = (ItemEJB) ctx.lookup("java:global/classes/ItemEJB");// Persists the book to the databasebook = itemEJB.createBook(book);assertNotNull("ID should not be null", book.getId());}}// Retrieves all the books from the databaseList books = itemEJB.findBooks();assertNotNull(books);As you can see in the initContainer() method in Listing 7-19, EJBContainer contains a factorymethod (createEJBContainer()) for creating a container instance. By default, the embeddable containersearches the client’s classpath to find the set of EJBs for initialization. Once the container has been229

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!