Developers, Developers, Developers! Maksim Sorokin IT Blog


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;

public class AppParamsLoader {

  private Properties props = null;

  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;

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

  public static void tearDown() throws Exception {

  public void testAppParamsLoader() {
    // ..

There is a way to initialize the container through maven:

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.