Over a million developers have joined DZone.

Improving the Testability of Java EE With Arquillian; 1.0.0 Alpha 1 Released

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

At Devoxx '09, JBoss introduced a framework that could help improve the testability of Java EE and JBoss AS.  Integration testing is important in Java EE because business components often interact with the container's resources or sub-system and many declarative services get applied to the business component at runtime.  Although most business logic can now be tested outside of the container because they are POJOs (as of JEE 5), many of the infrastructure services (transactions, security, etc.) were pushed into declarative programming constructs in order to isolate the JEE business logic from the services.  The Arquillian framework for running tests in the container helps confirm that the business logic is functioning properly within the context and it checks to see if the infrastructure services are applied correctly.  The 1.0.0 alpha 1 release of Arquillian is now available along with a wealth of information about the new technology.  Like most of their new projects, JBoss has licensed Arquillian under the ASL v2.

Arquillian excels in checking the integration layer that exposes the business logic in an enterprise Java application.  It is used as an extension for TestNG and JUnit, and you can liken it to a modern version of Cargo, but with a lot more potential.  

Arquillian hooks into your testing framework's lifecycle and starts reacting to events to manage the container (start/stop).  It also bundles the test class into a deployable archive along with dependent classes and resources.  Deploying the archive to test (deploy/undeploy) and capturing results along with failures are the other things that Arquillian handles.  

Arquillian defines two styles of container: remote and embedded.  There is no container specific configuration, so as of alpha 1 you can run test cases with Arquillian in GlassFish (embedded), JBoss AS (remote), OpenEJB (embedded), or in the Weld SE bean container (embedded).  You can also test on multiple platforms. JBoss intends to add more supported containers in future releases.

The following snippet is an example test case setup example using Arquillian with JUnit:
@RunWith(org.jboss.arquillian.junit.Arquillian.class)
public class TemperatureConverterTestCase {

@Deployment
public static JavaArchive createTestArchive() {
return Archives.create("test.jar", JavaArchive.class)
.addClasses(TemperatureConverter.class, TemperatureConverterBean.class);
}

}
The @RunWith annotation tells JUnit to use Arquillian as the test controller.  Arquillian then looks for a static method with the @Deployment annotation, which creates a micro deployment (more about them in a minute).  The example above was a session bean interface and implementation deployment.

Once a test case is started in the local JVM, Arquillian overrides the normal test execution and moves the test into the container, where it is executed.  Since the test framework is running in the container by the time it calls @Test, you are able to work with the container's managed resources.  Below is an example of a complete test class with JUnit @Test methods:
@RunWith(org.jboss.arquillian.junit.Arquillian.class)
public class TemperatureConverterTestCase {

@Deployment
public static JavaArchive createTestArchive() {
return Archives.create("test.jar", JavaArchive.class)
.addClasses(TemperatureConverter.class, TemperatureConverterBean.class);
}

@EJB
private TemperatureConverter converter;

@Test
public void shouldConvertToCelsius() {
Assert.assertEquals(converter.convertToCelsius(32d), 0d);
Assert.assertEquals(converter.convertToCelsius(212d), 100d);
}

@Test
public void shouldConvertToFarenheit() {
Assert.assertEquals(converter.convertToFarenheit(0d), 32d);
Assert.assertEquals(converter.convertToFarenheit(100d), 212d);
}
}
You can even use @EJB to inject the session bean from your deployment into the test case and use it in your test method.  All injection annotations from JEE6 (@EJB, @Resource, @Inject) are supported by Arquillian.

The big advantage of Arquillian is the way it facilitates the testing of Java enterprise applications in the environment that they will eventually run in.  This method tends to be better than introducing mock objects that won't always behave like the real ones.  

Another advantage of Arquillian is its ability to create "micro deployments" around your test.  These are sub-sections of your application logic.  The fluent API provided by the ShrinkWrap project makes this technique possible.  Micro deployments let you conduct testing on low level integrations and the ShrinkWrap API gives you fine-grained control over which resources are available to be tested.  

To learn more about Arquillian, check out the well-written documentation, which includes a guide for setting up Arquillian - no build wizardry required!  Now you have no excuse not to exercise TDD.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}