Java Reference
In-Depth Information
The next thing to look at in this integration test is the
setUpClass()
method annotated
with
@BeforeClass
. This method is called by JUnit before an instance of the test
class is created to set up any static resources the test needs—in this case, it's going to start
the embedded
EJBContainer
. To configure the embedded
EJBContainer
, a
Map
is
created and the
org.glassfish.ejb.embedded.glassfish.instance.root
property is put in the
Map
. The value is set as
./src/test/domain
. This con-
figures the embedded container with the directory where the GlassFish configuration
files are found. This configuration is nonportable and specific to only the GlassFish
implementation of the embedded
EJBContainer
. Once the configuration is defined,
the
last
thing
the
setUpClass()
method
does
is
call
EJBContain-
to start the embedded container.
er.createEJBContainer(properties)
At this point all of the hard work of the integration test is done. It's typically much harder
to get the embedded container configured and running than it is to actually use it to perform
tests.
Now we'll look at the
userGetsGoldDiscount()
test method . Recall that the
same test method is used in
listing 15.2
in the unit test. Although both the unit test and the
integration test will essentially be performing the same test, they each do it in drastically
different ways. The unit test uses mocks, avoids the EJB container, and tests
Discoun-
tManagerBean
as a POJO. The integration test will attempt to execute
Discoun-
tManagerBean
in an embedded
EJBContainer
. Let's see what the integration test
needs to do this.
We configured an in-memory Derby database in
domain.xml
and bound it to
jdbc/
chapter15-ejb-embedded
in JNDI for the integration test. The embedded database
is created every time an integration test is run, so you need to get data into the database.
The
userGetsGoldDiscount()
test method creates a
Member
entity, sets its data,
and inserts it in the database . The same thing is done with the membership-level data
using the
MembershipLevel
entity . Now that the appropriate data is in the data-
base for the test, the
DiscountManagerBean findDiscount()
method is called
and the results are asserted against expected results.