DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Improving the Testability of Java EE With Arquillian; 1.0.0 Alpha 1 Released

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

Mitch Pronschinske user avatar by
Mitch Pronschinske
·
Mar. 11, 10 · Interview
Like (0)
Save
Tweet
Share
17.04K Views

Join the DZone community and get the full member experience.

Join For Free
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.
Java EE Java (programming language) Testing Alpha (finance) Business logic Container Test case

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • 19 Most Common OpenSSL Commands for 2023
  • Assessment of Scalability Constraints (and Solutions)
  • Implementing PEG in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: