Java Reference
In-Depth Information
It's important to remember when then DiscountManagerBean findDiscount()
method is called you're not simply calling a POJO method with mocked dependencies like
the unit test in listing 15.2 . In this integration test, when the findDiscount() method
is called, the test is executing a real EJB method from a real EJB container running against
a real database. So the integration test helps to verify not only that the DiscountMan-
agerBean is returning the correct value but that all the different technologies—CDI, EJB,
JPA, Database—are working together as they should.
Now that you've started the embedded EJBContainer and have run the integration test,
what's left in the test class? There are a few helper methods used to get the beans from the
embedded EJBContainer : lookupDiscountManager() , lookupMember-
Manager() , and lookupMembershipLevelManager() . They all work the same
way. They use the EjbName enum to tell the lookupEjb() method exactly which
bean to look up. The lookupEjb() method performs the JNDI lookup by first using the
moduleName value, and if that fails, then it tries to use a hard-coded "classes" value.
It tries both ways, because depending on how you run the tests with Maven, both are valid.
This concludes our coverage of integration testing using the embedded EJBContainer .
As you can tell from this brief example, the embedded EJBContainer is very powerful
and brings testing of your application to the next level. Using the EJBContainer directly
can be a little difficult, though. Its configuration is complex, which may discourage its use.
To deal with this complexity, integration testing tools such as Arquillian are available to
help make integration testing easier. Let's take a look at Arquillian next.
15.4. Integration testing using Arquillian
In the previous section we talked about integration testing using the embedded EJBCon-
tainer . Now we're going to look at doing basically the same thing but using an integ-
ration testing framework named Arquillian. Why would you want to even use Arquillian
if you can use the embedded EJBContainer ? Simply put, Arquillian makes integration
testing with embedded containers a little easier so you're able to concentrate more on the
integration tests itself and not on all the plumbing needed to get it working. Arquillian also
makes more complicated integration testing scenarios easier. For example, Arquillian has
shrink-wraps for multiple Enterprise application servers, which may be very useful if your
organization is in the middle of switching from one Enterprise server to another, or if you
Search WWH ::




Custom Search