Arquillian has support for multiple injection points like @EJB, @Resources and @Inject, but there are also non standard component model objects available within the Arquillian runtime that can be of useful during testing. In this tutorial we will learn how to expose these objects to the test case using the @ArquillianResource injection annotation.

By using the @ArquillianResource injection you can expose multiple internal objects such as Containers or Deployments which will be handled programmatically by your Test case.

Let's see an example of it:

package com.itbuzzpress.chapter8.test;


import java.util.List;

import org.jboss.arquillian.container.test.api.*;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Before;
import org.junit.Test;

import javax.inject.Inject;

import static org.junit.Assert.*;

import org.jboss.arquillian.junit.Arquillian;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;

import org.junit.runner.RunWith;

import com.itbuzzpress.chapter8.ejb.ManagerEJB;
import com.itbuzzpress.chapter8.entity.Customer;
import com.itbuzzpress.chapter8.entity.Request;
import org.jboss.arquillian.junit.InSequence;
 
@RunAsClient
@RunWith(Arquillian.class)

public class TestJPA {

 @Inject ManagerEJB ejb;

 @ArquillianResource
 private ContainerController controller;

 @ArquillianResource
 private Deployer deployer;

 @Before
 public void before() throws Throwable {
  System.out.println("Before Test");
 }
 @Deployment(name = "deploy-0", managed = false)
 @TargetsContainer("node-0")

 public static Archive < ? > createTestArchive() {
  return ShrinkWrap.create(WebArchive.class, "testjpa.war")
   .addPackage(Customer.class.getPackage())
   .addPackage(ManagerEJB.class.getPackage())
   .addAsResource("META-INF/persistence.xml")
   .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");


 }

 @Test
 @InSequence(-1)
 public void startServer() {
  controller.start("node-0");
  deployer.deploy("deploy-0");
 }


 @Test
 @InSequence(1)
 public void stopServer() {
  deployer.undeploy("deploy-0");
  controller.stop("node-0");


 }

 @Test
 public void test() {
  try {

   ejb.createCustomer("john", "park avenue 125", "jsmithATgmail.com", "+3955555");
   Customer customer = ejb.findCustomerByName("john");
   List < Customer > cc = ejb.findAllCustomers();
   System.out.println(cc);
   assertNotNull(customer);
   ejb.createRequest("john", 100);

   List < Customer > customerList = ejb.findAllCustomers();

   log("=======> Customer List ");
   for (Customer c: customerList) {
    log("Customer found: " + c);
    log("============> Request Accociated:");
    List < Request > requestList = ejb.findAllRequestsByCustomer(c.getName());
    for (Request r: requestList) {
     log("Request found: " + r);
    }
   }

  } catch (Exception e) {
   System.out.println(e.getClass());
   // TODO Auto-generated catch block
   e.getMessage();
  }
 }

 private void log(String string) {
  System.out.println(string);

 }

}

This Test class is derived from the TestJPA class contained in the Practical Java EE Development with WildFly. Compared with the source code available on github (https://github.com/fmarchioni/practical-javaee7-development-wildfly/tree/master/code/chapter8/javaee7-test) we have changed a couple of things:

First of all, the application server is not started/stopped automatically by Arquillian but you can control it by injecting the Application server as an @ArquillianResource:

@ArquillianResource
private ContainerController controller;

Next, also the Deployment of applications is managed programmatically by injecting another @ArquillianResouce:

@ArquillianResource
private Deployer deployer;

Finally, notice that in order to guarantee that the @Test are run in the proper order (at first the application server is started, then the application is deployed and finally the test() method is executed), we have decorated our @Test with @Insequence annotation:

@Test
@InSequence

As you can see, we have referenced our Container by using the node-0 alias. This needs to be defined in arquillian.xml where you specify the Container type and configuration:

<arquillian xmlns="http://jboss.org/schema/arquillian"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://jboss.org/schema/arquillian
        http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

	<defaultProtocol type="Servlet 3.0" />

	<container qualifier="node-0" mode="manual" default="true"  >

		<configuration>
			<property name="jbossHome">/home/jboss/wildfly-12.0.0.Final</property>
			<property name="managementPort">9990</property>
			<property name="managementAddress">localhost</property>
		</configuration>

	</container>

</arquillian>
0
0
0
s2smodern