users@glassfish.java.net

Re: [Fwd: [embedded] How to unit test an EJB with Maven ?]

From: Antonio Goncalves <antonio.goncalves_at_gmail.com>
Date: Fri, 24 Apr 2009 14:29:00 +0200

Hi all,
I'm resending this email to get some news about the embeddable container. I
haven't read anything about it, so I wonder it's not ready yet. Can you
confirm ? Any clearer idea about the road map ?

Something weird about the Maven repository. I've tried to use the latest b14
version, but just the poms get downloaded (not the jars). Any idea ?

<dependency>
    <groupId>org.glassfish.embedded</groupId>
    <artifactId>glassfish-embedded-all</artifactId>
    <version>3.0-Prelude-Embedded-b14</version>
    <scope>test</scope>
</dependency>

Thanks,
Antonio

PS : Don't know why but I couldn't send this email to the
users_at_embedded-glassfish.dev.java.net mailing list. Does it still exist ?


2009/3/5 Kenneth Saks <Kenneth.Saks_at_sun.com>

>
> On Mar 4, 2009, at 4:49 PM, Antonio Goncalves wrote:
>
> Thanks for this information Kenneth. Do you have any idea of the road map
> of this feature ?
>
>
> No specifics at this point other than it's definitely going to be supported
> for V3. There's a lot
> in EJB 3.1 so we're trying to make forward progress on as much as possible.
> Stay tuned !
>
> --ken
>
>
> Antonio
>
> 2009/3/4 Kenneth Saks <Kenneth.Saks_at_sun.com>
>
>>
>> On Mar 4, 2009, at 3:19 PM, Nazrul Islam wrote:
>>
>>
>> *From: *Antonio Goncalves <antonio.mailing_at_gmail.com>
>> *Date: *March 4, 2009 3:15:20 PM EST
>> *To: *users_at_embedded-glassfish.dev.java.net
>> *Subject: **[embedded] How to unit test an EJB with Maven ?*
>> *Reply-To: *users_at_embedded-glassfish.dev.java.net
>>
>>
>> Hi,
>> I'm trying to unit test an EJB 3.1 (no-interface view) called BookEJB with
>> the embedded container and Maven. I get a null pointer exception when I try
>> to get the context :
>>
>>
>>
>> EJBContainer ec = EJBContainer.createEJBContainer();
>> Context ctx = ec.getContext();
>>
>> I've added the following dependency to my project :
>>
>> <dependency>
>> <groupId>org.glassfish.embedded</groupId>
>> <artifactId>glassfish-embedded-all</artifactId>
>> <version>3.0-Prelude-Embedded-b11</version>
>> <scope>test</scope>
>> </dependency>
>>
>> Below you will find my test class which, unfortunately, doesn't work. Do
>> you know what I'm doing wrong ? In Ken's blog (
>> http://blogs.sun.com/kensaks/entry/embeddable_ejb) he first packages the
>> EJB in a jar and adds the jar to the classpath.
>>
>>
>>
>> Hi Antonio,
>>
>> We haven't implemented the EJB 3.1 embeddable API in Glassfish yet.
>> We'll keep the list posted on any
>> progress.
>>
>> --ken
>>
>>
>> I can't do that with Maven, so do we really need to package the EJB in a
>> jar first ?
>>
>> Thanks for your help,
>> Antonio
>>
>>
>> Test class
>> ----------------
>> 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
>> public void createBook() throws Exception {
>>
>> // 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);
>> }
>> }
>>
>>
>> The EJB
>> --------------
>> @Stateless
>> public class BookEJB {
>>
>> // ======================================
>> // = Attributes =
>> // ======================================
>>
>> @PersistenceContext(unitName = "chapter06PU")
>> private EntityManager em;
>>
>> // ======================================
>> // = Public Methods =
>> // ======================================
>>
>> public List<Book> findBooks() {
>> Query query = em.createNamedQuery("findAllBooks");
>> return query.getResultList();
>> }
>>
>> public Book findBookById(Long id) {
>> return em.find(Book.class, id);
>> }
>>
>> public Book createBook(Book book) {
>> em.persist(book);
>> return book;
>> }
>>
>> public void deleteBook(Book book) {
>> em.remove(em.merge(book));
>> }
>>
>> public void updateBook(Book book) {
>> em.merge(book);
>> }
>> }
>>
>>
>>
>>
>
>
> --
> --
> Antonio Goncalves (antonio.goncalves_at_gmail.com)
> Software architect
>
> Paris JUG leader : www.parisjug.org
> Web site : www.antoniogoncalves.org
> Blog: jroller.com/agoncal
> LinkedIn: www.linkedin.com/in/agoncal
>
>
>


-- 
--
Antonio Goncalves (antonio.goncalves_at_gmail.com)
Software architect
Web site : www.antoniogoncalves.org
Blog: agoncal.wordpress.com
Feed: feeds2.feedburner.com/AntonioGoncalves
Paris JUG leader : www.parisjug.org
LinkedIn: www.linkedin.com/in/agoncal