TomEE Maven Alien--Sample Project For Maven 3, Arquillian and TomEE 📎
Lets assume you would like to integration test the following EJB within TomEE (a Tomcat on stereoids which makes lots of your boilerplate superfluous) with Arquillian:
@Stateless
public class HelloBoundary {
public String hello(){
return "Good Morning: " + new Date();
}
}
Arquillian test code is server agnostic; the archive is created, deployed and the EJB HelloBoundary
is available for injection:
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
@RunWith(Arquillian.class)
public class HelloBoundaryIT {
@Inject
HelloBoundary cut;
@Deployment
public static WebArchive deploy(){
return ShrinkWrap.create(WebArchive.class).
addClasses(HelloBoundary.class).
addAsWebInfResource(EmptyAsset.INSTANCE,
ArchivePaths.create("beans.xml"));
}
@Test
public void injection() {
assertNotNull(cut);
assertNotNull(cut.hello());
assertTrue(cut.hello().startsWith("Good"));
}
}
Usually Java EE 6 projects are packaged as WARs. The only interesting part of the POM are the TomEE and Arquillian dependencies:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.3.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>apache-tomee</artifactId>
<version>1.5.0</version>
<classifier>plus</classifier>
<type>zip</type>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
The project tomee-alien was checked in into http://kenai.com/projects/javaee-patterns
[In the "Real World Java EE Patterns--Rethinking Best Practices" book (Second Iteration, "Green Book"), Arquillian was used for integration testing of Java EE-infrastructure dependent code.]
See you at Java EE Workshops at MUC Airport!