Adding Arquillian test classes to the project

Once you have the basic infrastructure ready for running the tests, you can start building the test cases. Add your test classes to the src/test folder in the Maven project structure. The following annotations will help you avail the Arquillian features in your test classes:

  • Annotate the class with the @org.junit.runner.RunWith(Arquillian.class) annotation. This tells JUnit to invoke Arquillian for running the tests.
  • Designate a public static method to return a deployable archive by annotating it with @org.jboss.arquillian.container.test.api.Deployment. This method should return an org.jboss.shrinkwrap.api.ShrinkWrap.ShrinkWrap archive. ShrinkWrap is an easy way to create deployable archives in Java, and Arquillian uses this API to build minimal deployable artifacts for running tests. You can learn more about ShrinkWrap at http://arquillian.org/guides/shrinkwrap_introduction.
  • Annotate all the methods that need to be tested with @org.junit.Test.

The following is an Arquillian test class example for your quick reference. This class performs integration tests on the department resource:

//Other imports are removed for brevity 
import org.junit.Test;  
import org.jboss.arquillian.container.test.api.Deployment; 
import org.jboss.arquillian.junit.Arquillian; 
import org.jboss.arquillian.junit.InSequence; 
import org.jboss.shrinkwrap.api.ShrinkWrap; 
import org.jboss.shrinkwrap.api.spec.WebArchive; 
 
@RunWith(Arquillian.class) 
public class DepartmentResourceTest { 
 
    public DepartmentResourceTest() { 
    } 
 
    //This method return minimal files that needs to be deployed 
    //for running integration test 
    //@Deployment(testable=true) : Runs within container(default) 
    //@Deployment(testable=false) : Runs outside of container 
    @Deployment(testable=true) 
    public static WebArchive createDeployment() { 
        return ShrinkWrap 
                .create(WebArchive.class, "arquilian-demo-test.war") 
                .addClasses(Department.class,ApplicationConfig.class, 
                            JPAResource.class, 
                            DepartmentResource.class) 
                .addAsWebInfResource("test-web.xml",  
                                      "web.xml") 
                .addAsResource("test-persistence.xml", 
                               "META-INF/persistence.xml"); 
    } 
 
    //Method to be tested 
    @Test 
    @InSequence(1) 
    public void testAddDeptResource() { 
        WebTarget target = ClientBuilder.newClient() 
                .target( 
        "http://localhost:8080/arquilian-demo-test/api/departments"); 
        // Crate a new dept. 
        Department dept = new Department(); 
        dept.setDepartmentId(new Short((short) 10)); 
        dept.setDepartmentName("HR"); 
        Department  deptResult = target.request("application/json"). 
                     post(Entity.json(dept), Department.class); 
     
        assertEquals("HR", deptResult.getDepartmentName()); 
    } 
 
   //Rest of the methods are removed for brevity 
} 

If you have used JUnit before the preceding test class implementation, it may look familiar to you, except for a couple of methods and APIs:

  • The createDeployment() method used in this example generates a web archive and deploys it to the container. You do not need to build and deploy an entire application for testing a specific API. The ShrinkWrap class exposes APIs to create a WebArchive. You can use the addClasses() method on the WebArchive to add only the required classes in the web archive file.
  • Use the WebArchive::addAsWebInfResource() API to specify different configuration files for testing. For instance, in the preceding example's API, calling WebArchive::addAsWebInfResource("test-web.xml", "web.xml") adds test-web.xml to the web archive in the place of web.xml. This feature allows you to specify a different set of deployment descriptors and configuration files for the purpose of testing. All the test-related resources, such as deployment descriptors and configuration files, are stored in the src/test/resources folder.
The complete source code for this example is available on the Packt website. You can download the example from the Packt website link that we mentioned at the beginning of this book, in the Preface. In the downloaded source code, see the rest-appendix-arquillian project to a get a feel for the end-to-end implementation.
..................Content has been hidden....................

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