Java Reference
In-Depth Information
base, you need to take testing to the next level. This next level is integration testing, and
we'll talk about it next.
15.3. Integration testing using embedded EJBContainer
Let's take a look at how you'll integration test the same DiscountManagerBean from
listing 15.1 . In the previous section we covered unit testing DiscountManagerBean .
Using JUnit and Mockito, you saw how easy it is to create data and mocks and use them in
the unit test to verify the results from the business methods. For integration testing, you're
going to verify the same results, but you're going to do it in a completely different way.
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 be-
ginning of the test with the help of the builder method on the EJBContainer object, as
follows:
EJBContainer ejbContainer = EJBContainer.createEJBContainer();
Once you have an instance of EJBContainer , you can use it to get a Context :
Context ctx = ejbContainer.getContext();
After you have a Context , you're free to look up any bean bound in JNDI. Getting an
instance of the bean means you're then free to call whatever methods you want for the test:
DiscountManager manager = (DiscountManager) ctx.lookup(
"java:global/chapter15-ejb-1.0/DiscountManagerBean");
This has been a quick, high-level overview of using the embedded EJBContainer . Al-
though starting and using the embedded container can be this simple, it rarely is. The setup
and configuration needed to get the embedded container functioning properly to support all
the EJBs in your application can be a difficult task. The effort is worth it, because integra-
tion testing provides valuable information about your application.
 
Search WWH ::




Custom Search