Integration testing of JAX-RS resources with Arquillian

Integration testing tests the interactions between the individual software components in a system. It helps you uncover faults with the integrated components of a system earlier in the lifecycle. In this section, you will learn how to develop and run integration tests for a JAX-RS web application.

A typical JAX-RS web application is comprised of various software components, such as databases, the persistence layer, business service implementation, and the client interface layer. The integration tests that you write for a JAX-RS application should test the interaction between all these components when putting them together to build the complete application.

Unit testing versus integration testing

A unit test is typically a test written by developers to verify a relatively small piece of code, and it should not depend upon any other components or external resources, such as the database. They are narrow in scope.

An integration test verifies that the different pieces of the system are in sync when they are put together to build a complete system. These tests typically require external resources, such as database instances or third-party APIs.

JBoss Arquillian is a powerful tool for the integration testing of Java EE applications. With Arquillian, you do not need to take care of setting up the environment, including the container for running integration tests. Arquillian lets you set up the container in three modes, as follows:

  • Remote: This container runs in a separate JVM
  • Managed: This container is functionally the same as the remote container, except that its lifecycle (starting and stopping) is managed by Arquillian
  • Embedded: This container resides in the same JVM as your test case

A container can be of any of the following types:

  • Servlet container: Examples of a servlet container are Tomcat and Jetty
  • Full-fledged Java EE application server: Examples of a full-fledged Java EE application server are JBoss AS, GlassFish, and WebLogic
  • Java SE contexts and Dependency Injection(CDI) environment: Examples of Java SE contexts and dependency injection (CDI) environment are OpenEJB and Weld SE

Let's learn how to use Arquillian for building integration tests for a JAX-RS application. The following diagram illustrates the high-level architecture of the application that we'll use in this example:

This example exposes the stateless session beans as RESTful web resources, and it uses the Java Persistence API (JPA) for database access. We will use Maven to build this example. In reality, you can use Arquillian with your favorite build tools, such as Ant, Maven, or Gradle.

The following discussion assumes that you have already created a Maven-based JAX-RS application. The source structure may look as follows:

-src/ 
    -main/ 
      -java/ [Contains all application Java source files] 
      -resources/ [Contains all application configuration files]  
      -webapp/ [Contains all web files] 
    -test/ 
      -java/ [Contains all test Java source files] 
      -resources/ [Contains all test configuration files here]  
-pom.xml [The Maven build file] 

If you are not sure how to build Maven-based JAX-RS applications, refer to the Building a simple RESTful web service application using NetBeans IDE section, in Chapter 3, Introducing the JAX-RS API.

Now, we will look at the steps for adding Aquillian to a Maven-based JAX-RS application.

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

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