concept embed ejbcontainer in category ejb

appears as: embedded EJBContainer, n embedded EJBContainer, The embedded EJBContainer, embedded EJBContainer
EJB 3 in Action, Second Edition

This is an excerpt from Manning's book EJB 3 in Action, Second Edition.

How does this relate to EJBs? Just as the database is mimicked for integration tests, the EJB container needs to be mimicked for testing your EJBs. Just as there are in-memory databases like Derby to mimic “real” databases like MySQL or Oracle, there’s an embedded EJBContainer that may be used to mimic a “real” Java EE server like GlassFish. Let’s take a look at this embedded EJBContainer.

The embedded EJBContainer is part of the EJB specifications and is required in a full EJB implementation. We briefly introduced the embedded EJBContainer in chapter 5 as a way for EJBs to be used within Java SE applications. The embedded EJBContainer can also be utilized by integration tests for testing EJBs. The integration test can start the embedded EJBContainer in-memory, which will deploy all the EJBs it finds in the integration test’s class path and then proceed with testing the EJBs. Although the embedded EJBContainer is a tremendous resource, using it can be quite a challenge. This is where technologies such as Arquillian come in.

Arquillian is a sophisticated integrated testing tool. Think of it as a wrapper around the embedded EJBContainer. It presents a simpler interface for configuring and running the embedded EJBContainer. This allows your integration testing to focus more on testing the EJBs than on resource management for running the integration tests.

Just like section 15.3 on integration testing using the embedded EJBContainer directly, you need to configure GlassFish because it’s the GlassFish implementation of EJBContainer that you’ll be using. For the Arquillian integration test, you need to provide the src/test/glassfish/glassfish-resources.xml file. This file defines a jdbc-connection-pool for GlassFish and binds it in JNDI to jdbc/chapter15-ejb-arquillian:

<!DOCTYPE resources PUBLIC
   "-//GlassFish.org//DTD GlassFish Application Server 3.1
    Resource Definitions//EN"
   "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <jdbc-resource pool-name="ArquillianEmbeddedDerbyPool"
    jndi-name="jdbc/chapter15-ejb-arquillian"/>
  <jdbc-connection-pool name="ArquillianEmbeddedDerbyPool"
    res-type="javax.sql.DataSource"
    datasource-classname="org.apache.derby.jdbc.EmbeddedDataSource"
    is-isolation-level-guaranteed="false">
    <property name="databaseName"
      value="target/derby/arquillian-integration-test"/>
    <property name="createDatabase" value="create"/>
  </jdbc-connection-pool>
</resources>


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":6,\"ch\":15},{\"line\":6,\"ch\":44}]]"}
!@%STYLE%@!

Notice that configuring GlassFish when using Arquillian is much different than when directly using the embedded EJBContainer. Using EJBContainer, you basically brought in the configuration for an entire GlassFish domain. Using Arquillian, you’re specifying only the resources you want, which in this case is only a database connection pool to Derby. The last technology you need to configure is JPA. Let’s take a look at the JPA configuration now.

The most important aspect of integration testing is to mimic the real execution environment as much as possible so you get an idea from the integration test how your application will run in a real environment. There are a few different ways to do this. This section will focus on using the embedded EJBContainer, which is an in-memory container that’s easily started by any Java SE application. You’re going to start the embedded container at the beginning of the test with the help of the builder method on the EJBContainer object, as follows: