15.1.12

EJB 3.1 Integration Test: Embedded Container

After testing your EJBs successfully using JUnit and EasyMock, its now time to go one testing step further. There are actually several possibilities how to perform Integration Tests for your EJBs, but this entry will concentrate on just one, namley using the since v 3.1 specified Embedded Container (Glassfish v3). It can be seen as something like a small Smoke Test; it consists of one Statless Session Bean and one Interceptor that changes the Bean's internal state.

If you are using Maven simply add the folling dependency entry:

 <dependency>
    <groupId>org.glassfish.extras</groupId>
    <artifactId>glassfish-embedded-all</artifactId>
    <version>3.1</version>
    <scope>test</scope>
</dependency>

I read about former issues and additional configurations when using Maven, but I did not run into any problems and just used the following code for my test:

import javax.ejb.embeddable.EJBContainer;
public class SomeEJBTest {

    private EJBContainer ejbContainer;
    private Context context;
    @Before
    public void setup() throws Exception{
        ejbContainer = EJBContainer.createEJBContainer();
        context = ejbContainer.getContext();
    }
    @After
    public void teardown(){
        ejbContainer.close();
    }
    @Test
    public void getsInterceptedCorrectly() throws Exception {
        SomeEjbLocal someEjb = 
               (SomeEjbLocal) context.lookup(
           "java:global/classes/SomeEJB!at.rt.ejbs.SomeEjbLocal");
        assertEquals(MyInterceptor.NUM, someEjb.getNum());
    }
}

The test ran through using either the IDE (Eclipse) and Maven and the container started within ~6 seconds. No remote interface was needed and it worked with implementation view and local interface. By specification the container  supports just the Web Profile, which should be sufficient in most cases.  However, here you can also download the full profile for the Glassfish v3 container. More Glassfish specific information can be found here.

No comments:

Post a Comment