[DevoxxFR2013] Arquillian for Simple and Effective Web Tests
Lecturer
Alexis Hassler is a Java developer with over fifteen years of experience. He operates as an independent professional, engaging in coding, training, and supporting enterprises to enhance their Java development and deployment practices. As co-leader of the Lyon Java User Group, he contributes to community events like the Mix-IT conference in Lyon.
Abstract
Alexis Hassler’s session explores Arquillian’s capabilities for testing web applications, extending beyond Java EE components to comprehensive integration testing. Demonstrating extensions like Drone, Graphene, and Warp, he illustrates client-side and server-side testing methodologies. The discussion analyzes setup, execution, and benefits, emphasizing real-world testing that verifies both client interactions and server states for robust applications.
Core Concepts of Arquillian: Integration Testing Reversed
Hassler introduces Arquillian as a tool for integration testing Java EE components, defining a component as an entity like an EJB with injected dependencies, transactional, and security contexts. Unlike embedding containers in tests, Arquillian packages the component with tests and deploys to a container, inverting the process for realistic environments.
A basic test class uses JUnit with an Arquillian runner. The @Deployment annotation creates a testable archive (e.g., WAR) containing classes and resources. Methods like @Test execute within the container, accessing injected resources seamlessly.
This approach ensures tests run in production-like settings, catching issues early. Hassler contrasts with traditional unit tests, highlighting Arquillian’s focus on integrated behaviors.
Extending to Web Testing: Drone and Graphene for Client-Side Interactions
For web applications, Hassler leverages Drone and Graphene extensions, combining Arquillian’s deployment with Selenium’s browser automation. Drone instantiates WebDriver instances; Graphene enhances with guards for AJAX handling.
In setup, specify a managed container like JBoss AS in arquillian.xml. Tests deploy the app, then use @Drone-annotated browsers to interact: navigate via @ArquillianResource URLs, fill forms, assert outcomes.
Graphene’s guards wait for requests, ensuring reliable tests amid asynchronous behaviors. This client-mode testing (@RunAsClient) simulates user interactions, verifying HTTP responses without server-side access.
Code example:
@Drone
WebDriver browser;
@ArquillianResource
URL deploymentUrl;
@Test
@RunAsClient
public void testLogin() {
browser.get(deploymentUrl + "login.jsf");
// Interact and assert
}
This deploys, browses, and validates, bridging unit and functional testing.
Advanced Verification with Warp: Bridging Client and Server States
To inspect server-side states during client tests, Hassler introduces Warp. It wraps requests with payloads, executing inspections on the server before/after phases.
Define inspections extending WarpInspection, annotating with @WarpTest. Use @BeforeServlet/@AfterServlet for timing. Deploy via @WarpDeployment.
Example: Verify session attributes post-login. Client triggers action; Warp asserts server-side.
public class SessionInspection extends WarpInspection {
@AfterServlet
public void checkSession() {
// Assert session values
}
}
Warp enables “real tests” by combining client simulations with server validations, detecting mismatches invisible to pure client tests.
Hassler notes Warp’s alpha status, advising community forums for issues.
Implications for Development Practices: Flexibility and Community Support
Arquillian’s modularity allows mixing modes: @RunAsClient for web, in-container for components. This flexibility suits diverse apps, from simple servlets to complex JSF.
Challenges include initial setup—choosing extensions, versions, resolving conflicts. Documentation is evolving; forums on JBoss.org are responsive, often from developers.
Hassler encourages adoption for effective testing, reducing deployment surprises. Community involvement accelerates learning, fostering better practices.
In essence, Arquillian with extensions empowers thorough, efficient web testing, aligning development with production realities.