Arquillian

Language: Java

Testing/Integration

Arquillian was created to overcome limitations of traditional unit testing frameworks for enterprise applications. It provides a way to deploy your code in a container, run tests against it, and verify real-world interactions, making integration testing easier, repeatable, and closer to production scenarios.

Arquillian is a powerful testing platform for Java that simplifies integration and functional testing of Java EE and Jakarta EE applications. It allows developers to execute tests inside real runtime containers rather than mocks, ensuring realistic behavior and deployment conditions.

Installation

maven: Add dependencies in pom.xml: <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <version>1.7.0.Final</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.container</groupId> <artifactId>arquillian-weld-embedded</artifactId> <version>3.0.0.Final</version> <scope>test</scope> </dependency>
gradle: Add dependencies in build.gradle: testImplementation 'org.jboss.arquillian.junit:arquillian-junit-container:1.7.0.Final' testImplementation 'org.jboss.arquillian.container:arquillian-weld-embedded:3.0.0.Final'

Usage

Arquillian allows developers to write tests that run inside real or embedded containers. It manages the deployment lifecycle, container startup, and test execution, and integrates with popular testing frameworks like JUnit and TestNG.

Simple Arquillian test with JUnit

import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class MyServiceTest {

    @Deployment
    public static JavaArchive createDeployment() {
        return ShrinkWrap.create(JavaArchive.class)
                         .addClass(MyService.class)
                         .addAsManifestResource("beans.xml");
    }

    @Test
    public void testServiceMethod() {
        MyService service = new MyService();
        assertEquals("Hello", service.sayHello());
    }
}

Defines a deployment package using ShrinkWrap and runs a simple test in the Arquillian-managed container.

Injecting CDI beans

import javax.inject.Inject;

@Inject
MyService service;

@Test
public void testInjectedService() {
    assertEquals("Hello", service.sayHello());
}

Demonstrates dependency injection in tests to access container-managed beans.

Using different container adapters

// In arquillian.xml, configure container:
// <container qualifier="wildfly" default="true">
//     <configuration>
//         <property name="managementAddress">localhost</property>
//         <property name="managementPort">9990</property>
//     </configuration>
// </container>

Arquillian supports multiple containers such as WildFly, GlassFish, and TomEE, configured via arquillian.xml.

Integration with TestNG

@RunWith(Arquillian.class)
public class MyTestNGTest extends AbstractTestNGSpringContextTests {
    // TestNG methods with @Test annotations
}

Arquillian can also integrate with TestNG instead of JUnit.

Error Handling

DeploymentException: Occurs when the deployment archive is invalid or container fails to start. Verify ShrinkWrap configuration and container availability.
InjectionException: Occurs when CDI beans cannot be injected. Ensure beans.xml is present and CDI beans are properly defined.
ContainerStartupException: Occurs if the container cannot start. Check container installation, network settings, and version compatibility.

Best Practices

Use ShrinkWrap to define minimal deployment packages containing only required classes and resources.

Leverage embedded containers for fast, repeatable tests during development.

Use Arquillian Drone or Selenium integration for functional web testing.

Separate unit tests and integration tests clearly to maintain test speed.

Configure container-specific settings in arquillian.xml to match production environments.