Chapter 6 - BookEJBTest.java
package com.apress.javaee6.chapter06;

import static org.junit.Assert.assertNotNull;
import org.junit.Ignore;
import org.junit.Test;

import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;
import java.util.List;

/**
 * @author Antonio Goncalves
 *         APress Book - Beginning Java EE 6 with Glassfish
 *         http://www.apress.com/
 *         http://www.antoniogoncalves.org
 *         --
 */
public class BookEJBTest {

    // ======================================
    // =             Attributes             =
    // ======================================
    private static EJBContainer ec;
    private static Context ctx;

    // ======================================
    // =          Lifecycle Methods         =
    // ======================================

//    @BeforeClass
//    public static void initContainer() throws Exception {
//        ec = EJBContainer.createEJBContainer();
//        ctx = ec.getContext();
//    }
//
//    @AfterClass
//    public static void closeContainer() throws Exception {
//        ec.close();
//    }

    // ======================================
    // =              Unit tests            =
    // ======================================

    @Test
    @Ignore("EJBContainer not implemented yet")
    public void createBook() throws Exception {

        ec = EJBContainer.createEJBContainer();
        ctx = ec.getContext();

        String tel = "+33";
        if (!tel.startsWith("+")) {

        }

        // Creates an instance of book
        Book 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 EJB
        BookEJB bookEJB = (BookEJB) ctx.lookup("java:global/BookEJB");

        // Persists the book to the database
        book = bookEJB.createBook(book);
        assertNotNull("ID should not be null", book.getId());

        // Retrieves all the books from the database
        List<Book> books = bookEJB.findBooks();
        assertNotNull(books);

        ec.close();
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License