Chapter 6. Testing Your Applications

We have now reached a good mix of technologies in our example project; this includes some of the key building blocks of Java EE (except for Java Messaging, which will be discussed in the next chapter). Besides developing applications, one of the most common requests among developers is the ability to execute test cases against applications deployed on a managed (or remote) application server. In this chapter, we will describe the JBoss AS framework named Arquillian that promises to be the standard integration testing framework of enterprise applications.

In this chapter, we will more specifically look at:

  • An introduction to enterprise testing: from mock objects to the Arquillian framework
  • How to integrate an Arquillian test case for your Ticket Machine application
  • How to use the Eclipse IDE and the Maven shell to run Arquillian unit testing

Unit testing and integration testing

The word "testing" offers room for several interpretations; basically, testing requires verifying the application's basic functionalities. However, there can be different types of tests depending on what you are testing and what environment you are using for testing.

The most common type of test is called a unit test and can be defined as a test written by the programmer to verify that a relatively small piece of code is doing what it is intended to do. Unit tests are narrow in scope; they should be easy to write and execute, and their effectiveness depends on what the programmer considers to be useful. These tests are intended for the use of the programmer; they are not directly useful to anybody else, though, if they do their job, testers and users downstream should benefit by seeing fewer bugs.

A more advanced type of test is called the integration test . Integration tests are done to demonstrate that different pieces of the system work together; since they cover whole applications, they require much more effort to put together. For example, they usually require resources such as database instances and hardware to be allocated for them. Integration tests do a more convincing job of demonstrating how the system works (especially to nonprogrammers), at least to the extent that the integration test environment resembles the production environment.

Instruments for testing

As you can imagine, each kind of testing uses a different approach; for example, as far as unit testing is concerned, the most common way to test is by means of mock objects . If you have an object whose methods you want to test, and if these methods depend on some other object, you can create a mock of the dependency rather than an actual instance of that dependency. This allows you to test your object in isolation.

As an example, one common use case might be in an MVC application, where you have a DAO (data access objects) layer and a controller that performs business logic. If you'd like to unit test the controller, and the controller has a dependency on a DAO layer, you can make a mock of the DAO that will return dummy objects to your controller.

This kind of approach, although very immediate to understand and put in to practice, has several limitations. Firstly, it relegates you into an artificial environment where you will often make invalid assumptions about the behavior and stability of that environment.

Secondly, you will end up with a hard-to-maintain mock library that will allow your tests to pass and give you the warm feeling of having done a great job.

So, even if mock objects may still provide some benefits for starting up systems, where you don't have full implementations of a particular subsystem, you should stick, as close as possible, to the target environment that the code is supposed to run in.

Arquillian is a platform that simplifies integration testing for Java middleware. It deals with all the plumbing of container management, deployment, and framework initialization so you can focus on the task of writing your tests—real tests. Arquillian minimizes the burden on you—the developer—by covering aspects surrounding test execution; some of these aspects are as follows:

  • Managing the life cycle of the container (start/stop)
  • Bundling the test class with the dependent classes and resources into a deployable archive
  • Enhancing the test class (for example, resolving @Inject, @EJB, and @Resource injections)
  • Deploying the archive to test applications (deploy/undeploy), and capturing results and failures

In the next section, we will show which instruments are required to run your integration tests using Arquillian.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.5.15