Integration Tests with Arquillian

Since the early days of Java Enterprise Edition, testing of container components and their interaction has been a pain-point. Their cooperation with other services has been traditionally difficult to simulate. With the increasing functionality of the container (dependency injection, etc.) cross-links and dependencies have also grown. A fully automated integration test suite previously consisted of many JUnit tests. Now, Arquillian aims to make this task easier by offering the first complete and integrated test suite for integration testing.

The mission of the JBoss project Arquillian is easy. It's about providing a simple test framework. The aim is to remove all container lifecycle and deployment tasks from the actual test logic and enable developers to build a broad range of integration tests for their enterprise Java applications. Integration tests should be a breeze with Arquillian. Since the early days of Java EE, testing enterprise applications has been a major challenge for any project. The isolated testing of technical components was impossible in some cases, since simple unit tests were frequently insufficient. One main reason for this, was the design of the components. They are rarely completely isolated and often depended on both their runtime and other components.

In the overall context, these aspects are as important as the technical work performed by a component. Even with a clean layer separation, a combination of so-called Mocks (interface simulation) and unit testing is required to integration test the interfaces and the implemented logic of the components. And despite all the potential problems, the actual behaviour of the target container remains completely untested. Neither transaction control, dependency injection, or other declarative services can be tested adequately. It is usually one of those final, open questions whether the right services are bound or the transferred data is actually in the proper format. Ensuring this, however, is an essential part of development processes in companies, and it is known as integration testing. The art of designing an automated integration test and regularly carrying them out, is something only a few experienced developers are able to do. This results in many stability problems for projects. The JBoss project Arquillian tries to eliminate this deficiency. It allows developers to easily run integration tests for container components. It does not matter whether these are deployed on a local (embedded) or remote container. The idea behind Arquillian is to make integration tests as simple as unit tests.

General Architecture

Arquillian (Figure 1) connects a unit testing framework (JUnit or TestNG), ShrinkWrap, and one or more supported containers (Java EE container, servlet container, Java SE CDI environment, etc.).

Integration Tests with Arquillian_第1张图片

In essence it's a custom test runner for JUnit and TestNG, which transfers control of the test lifecycle of the respective test implementation to Arquillian. From there Arquillian delegates to the respective environments for testing within the container or against the runtime. An Arquillian test case still looks almost exactly like a normal JUnit or TestNG test case. The testcases basically cover three different aspects. The testcase itself, the deployment, which has to be packaged into an archive and, last but not least, the deployment to the runtime.

Neither the deployment process nor the actual container control is very magical. The packaging of the testcases into the right format for the target runtime is done by ShrinkWrap. This is also the true start of an Arquillian test lifecycle. It defines the basis of the test case, a suitable archive at runtime and deploys it on the target container. After that the actual execution of the test case is initiated and results are collected. Finally, the archive will be removed from the runtime (undeployed). Up to now, there are only a couple of open-source containers supported. JBoss (> = 5), GlassFish (> = 3), Jetty (>= 6.1), Tomcat 6, Weld SE and EE 1.0 or 1.1, 1.0, IronJacamar 1.0, OpenWebBeans Apache 1.0 and Apache OpenEJB 3.1 are among them. According to the manufacturer future support will also cover commercial platforms such as Oracle's WebLogic or IBM's WebSphere.

原文地址:http://jaxenter.com/integration-tests-with-arquillian-35990.html。