Mitch Pronschinske is a Senior Content Analyst at DZone. That means he writes and searches for the finest developer content in the land so that you don't have to. He often eats peanut butter and bananas, likes to make his own ringtones, enjoys card and board games, and is married to an underwear model. Mitch is a DZone Zone Leader and has posted 2573 posts at DZone. You can read more from them at their website. View Full User Profile

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

03.11.2010
| 14117 views |
  • submit to reddit
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.

Comments

Ryan Developer replied on Thu, 2010/03/11 - 6:34pm

Wow.  First Seam, then JSFUnit, and now Arquillian.  JBoss sure is serious about Java EE -- thank you!

Evan Ghosh replied on Mon, 2011/01/17 - 8:14pm


 

I think one of the main downsides of Java EE is its testability. Arquillian has really solved that issue to a great extent. The reason so is because of its efficiency in monitoring the integration layer. Also another notable feature of Arquillian is its ability to define two varieties of container which are embedded and remote. I have often found Arquillian to drastically increase the testing of applications from the Java enterprises! The fact that it even creates micro deployments makes it a must have!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.