Monday, April 7, 2008

Fifth Disease In Blacks

Testing EJB3 beans with

After the first skirmishes of the JPA is time for gain first experiences in the writing of EJB3 beans. But first I wanted to explore issues related to testing of these grains. In previous versions of EJB testing was pretty hard, because as we all know the grain, to work had to be embedded in the EJB container, which were only available in application servers. As for me, this is too complicated and too time-consuming process of testing. Unfortunately, EJB3 beans also need to run them in an EJB container.

Wbudowywalny EJB3 container

For some time, make JBoss EJB3 container wbudowywalny . It is a container that can be run outside the application server. At present, the container has indeed some limitations, but as long as access to all services required by me I'm not going to be too bothered;)
However koncepcja wbudowywalnego EJB3 container lets you create, fast and easy to use unit testing environment for EJB3 beans. EJB3 test environment

When creating a unit test environment for the beans will use the said wbudowywalny EJB3 container.
To run is required in order to reach classpath'a were (in addition to the implementation of the container and the dependent libraries), the following configuration files: embedded-jboss-beans.xml , ejb3-interceptors -aop.xml , jndi.properties , default.persistence.properties .
In order to avoid placing these files in all subprojects EJB modules, forming a corporate application, and determination in each of them depending on the EJB3 container jar'ów built, I decided to create a dedicated project maven'a. Draft Environmental unit testing EJB will include these configuration files, which are defined according to jar'ów implement container and test base class for tests individual grains.
Here's implementation unit test base class: package
 pl.dwalczak.ejb3testenv; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.log4j.Logger; import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap imports, org.jboss.ejb3.embedded.EJB3StandaloneDeployer; org.testng.annotations.AfterSuite import, import org.testng.annotations.BeforeSuite; abstract public class {private static Ejb3Test final Logger LOG = Logger.getLogger (Ejb3Test.class); private static final String LOCAL_POSTFIX = "/ local"; private InitialContext InitialContext; private EJB3StandaloneDeployer deployer; @ BeforeSuite startupEjb3 public void ()  {
EJB3StandaloneBootstrap.boot(null);
EJB3StandaloneBootstrap.scanClasspath();

deployer = EJB3StandaloneBootstrap.createDeployer();
try {
deployer.setKernel(EJB3StandaloneBootstrap.getKernel());
deployer.getArchivesByResource().add("META-INF/ejb-jar.xml");
deployer.create();
deployer.start();
initialContext = new InitialContext();
} catch (NamingException e) {
LOG.error("Can't initialize context.", e);
throw new RuntimeException(e);
} catch (Exception e) {
LOG.error("Deployer error.", e);
throw new RuntimeException(e);
}
}

@AfterSuite
public void shutdownEjb3() {
if (null != initialContext) {
try {
initialContext.close();
} catch (NamingException e) {
LOG.error("Can't close context.", e);
}
}
if (null != deployer) {
try {
deployer.stop();
deployer.destroy();
} catch (Exception e) {
LOG.error("Can't close deployer.", e);
}
}
EJB3StandaloneBootstrap.shutdown();
}

@SuppressWarnings(value = "unchecked")
public T lookup(String beanName, Class businessInterface) {
T result = null;
try {
result = (T) initialContext.lookup(beanName + LOCAL_POSTFIX);
} catch (NamingException e) {
LOG.error("Can't bean lookup: "+ beanName, e) throw new RuntimeException (e);} return result;}}
This adaptation of the new EJB module in maven'ie will be to add the dependencies for this project (the scope set to test).

It should be noted that a more complex enterprise application is likely to be required specially tailored project test environment. But I think I have prepared a draft of that environment is a good starting point.

Testing EJB3 beans - such

In my example, we have dealing with a very simple seeds:
 pl.dwalczak.ejb3pg1 package; javax.ejb.Stateless import, import org.apache.log4j.Logger; @ Stateless public class Calculator implements CalculatorBean {private static Logger LOG = Logger.getLogger (CalculatorBean.class) public int add ( int x, int y) {LOG.debug ("add (" + x + "" + y + ")"); return x + y;}} 
business interface: package
 pl.dwalczak.ejb3pg1; public interface Calculator {int add (int x, int y);} 
After adding the project according to the test environment, you can write a unit test for this grain, which might look as follows:
 pl.dwalczak.ejb3pg1 package imports, org.testng.Assert; org.testng.annotations.Test import, import pl.dwalczak.ejb3testenv.Ejb3Test; public class extends CalculatorTest Ejb3Test {@ Test public void testAdd () {Calculator calculator = lookup ("CalculatorBean" Calculator.class) calculator.add int sum = (2, 2); Assert.assertEquals (sum, 4);}} 
is just one more question. Do you need to add the EJB module configuration file META-INF/ejb-jar.xml, even if you do not need to configure anything in it. It is simply necessary that the container used for grain zaczytał EJB3 class even though they are configured using annotations.

Resources

Example Source Files Source files
ejb3-test-env
EJB 3.0 Embeddable
JSR-000220 Enterprise JavaBeans 3.0
JavaTM Platform, Enterprise Edition, v 5.0 API Specifications

0 comments:

Post a Comment