Developers, Developers, Developers! Maksim Sorokin IT Blog

10Feb/10Off

Java EE 6 EJB Integration Testing

Testing EJBs in a container!

Say, you have a @Singleton

package example;

import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;

@Singleton
public class AppParamsLoader {

  private Properties props = null;

  @PostConstruct
  void init() {
    props = loadProperties();
  }

  public String getParam(String paramKey) {
    return props.getProperty(paramKey);
  }

  void loadProperties() {
    // ..
  }
}

You can use EJBContainer for your integration testing:

package example;

import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class AppParamsLoaderTest {

  private static EJBContainer container;
  private static Context namingContext;

  private static AppParamsLoader appParamsLoader;

  @BeforeClass
  public static void setUp() throws Exception {
    container = EJBContainer.createEJBContainer();
    namingContext = container.getContext();
    appParamsLoader = (AppParamsLoader) namingContext.lookup("java:global/classes/AppParamsLoader");
  }

  @AfterClass
  public static void tearDown() throws Exception {
    container.close();
  }

  @Test
  public void testAppParamsLoader() {
    // ..
  }
}

There is a way to initialize the container through maven:
http://blogs.sun.com/alexismp/entry/glassfish_embedded_and_javadb_embedded

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.