0% found this document useful (0 votes)
2 views16 pages

JavaEE Testing Section9

This document provides an overview of testing Java EE applications using the Arquillian framework, which allows for in-container testing to ensure application correctness and reliability. It discusses the challenges of Java EE testing, the core features of Arquillian, and provides examples of test structure and dependency setup. The document emphasizes the benefits of using Arquillian for real environment testing and accurate resource management in Java EE applications.

Uploaded by

csesanjay17
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

JavaEE Testing Section9

This document provides an overview of testing Java EE applications using the Arquillian framework, which allows for in-container testing to ensure application correctness and reliability. It discusses the challenges of Java EE testing, the core features of Arquillian, and provides examples of test structure and dependency setup. The document emphasizes the benefits of using Arquillian for real environment testing and accurate resource management in Java EE applications.

Uploaded by

csesanjay17
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Testing Java EE –

Section 9 Overview
Testing Java EE – Section 9 Overview

• Understand how to test Java EE applications effectively.


• Learn Arquillian testing framework integration for in-
container testing.
• Topics: Arquillian introduction, dependency setup, failing
& passing tests, Java EE resources.
Java EE Testing - An Introduction
• Testing ensures application correctness, stability, and
reliability.
• Java EE components run inside containers, making
traditional JUnit testing harder.
• Arquillian bridges the gap by running tests inside a real
Java EE container.
Challenges in Testing Java EE

• Java EE tests need container-managed resources (JPA,


CDI, EJB, etc.).
• Mocking or simulating containers can be complex.
• Solution: Arquillian provides integration with real servers
for true environment testing.
Introducing Arquillian

• Arquillian is a testing platform for Java EE (by JBoss/Red


Hat).
• Allows you to run tests inside containers like WildFly,
Payara, or GlassFish.
• Integrates seamlessly with JUnit and Maven.
• Simplifies setup and automates deployment of test
archives.
Arquillian Core Features

• Runs tests inside real Java EE runtime.


• Supports multiple containers (managed, remote,
embedded).
• Integrates with JUnit, TestNG, and Maven.
• Simplifies dependency injection and resource
management.
Dependency Setup
Add Arquillian dependencies in Maven ([Link]):
• <dependency>
• <groupId>[Link]</groupId>
• <artifactId>arquillian-junit-container</artifactId>
• <version>[Link]</version>
• </dependency>
• Add container adapter (e.g., WildFly, Payara).
Arquillian Test Structure

1. Create deployment method with ShrinkWrap.


2. Write test methods annotated with @Test.
3. Arquillian deploys and runs tests in the container.
Example structure:
@RunWith([Link])
public class MyTest { ... }
Folder Structure

ArquillianDemo/
├── src/main/java/com/example/service/
│ └── [Link]
├── src/test/java/com/example/test/
│ └── [Link]
├── [Link]
1. [Link] (Maven Dependencies)
<project xmlns="[Link] <version>[Link]</version>
xmlns:xsi="[Link] <scope>test</scope></dependency>
xsi:schemaLocation="[Link]
[Link] <!-- JUnit -->
<modelVersion>4.0.0</modelVersion> <dependency>
<groupId>junit</groupId>
<groupId>[Link]</groupId> <artifactId>junit</artifactId>
<artifactId>ArquillianDemo</artifactId> <version>4.13.2</version>
<version>1.0</version> <scope>test</scope>
<dependencies> </dependency>
<!-- Arquillian Core (JUnit Integration) --> <!-- Java EE APIs -->
<dependency> <dependency>
<groupId>[Link]</groupId> <groupId>javax</groupId>
<artifactId>arquillian-junit-container</artifactId> <artifactId>javaee-api</artifactId>
<version>8.0</version>
<version>[Link]</version> <scope>provided</scope>
<scope>test</scope> </dependency>
</dependency> </dependencies>
<!-- WildFly Managed Container Adapter --> </project>
<dependency>
<groupId>[Link]</groupId>
<artifactId>jboss-as-arquillian-container-
managed</artifactId>
3. Arquillian Configuration (Optional)
If you use a managed container like WildFly, add a
2. [Link] (Business Logic configuration file:
Class) <?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="[Link]
package [Link]; xmlns:xsi="[Link]
instance"
import [Link]; xsi:schemaLocation="[Link]
@Stateless [Link]
public class CalculatorService { <container qualifier="wildfly" default="true">
public int add(int a, int b) { <configuration>
<property name="jbossHome">C:/wildfly-
return a + b; [Link]</property>
<property name="managementPort">9990</property>
} <property name="username">admin</property>
public int multiply(int a, int b) { <property name="password">admin</property>
</configuration>
return a * b; </container>
} </arquillian>

}
4. [Link] (Arquillian Test) // 🧩 2. Inject the EJB to be tested
package [Link]; @EJB
import [Link]; private CalculatorService calculatorService;
// ❌ 3. A failing test example
import [Link];
@Test
import [Link]; public void should_fail() {
import [Link]; int result = [Link](2, 2);
import [Link]; [Link](5, result); // purposely
import [Link]; fail condition
}// ✅ 4. A passing test example
import [Link];
@Test
import [Link]; public void should_add_numbers_correctly() {
import [Link]; int result = [Link](2, 3);
import [Link]; [Link](5, result);
@RunWith([Link]) }
public class CalculatorServiceTest {
@Test
public void should_multiply_numbers_correctly() {
// 1. Define what gets deployed to the container int result = [Link](3, 4);
@Deployment [Link](12, result);
public static JavaArchive createDeployment() { }
return [Link]([Link]) }
.addClass([Link])
.addAsManifestResource([Link],
"[Link]"); // CDI enable }
Failing a Test Example

@Test
public void should_fail() {
int result = 2 + 2;
assertEquals(5, result); // Test fails
}
• Shows assertion failures with container-managed
environment.
Passing a Test Example

@Test
public void should_pass() {
int result = 2 + 2;
assertEquals(4, result); // Test passes
}
• Demonstrates successful validation of business logic.
Java EE Resources in Tests

• @EJB, @Inject, @PersistenceContext can be used


directly in Arquillian tests.
• Arquillian automatically injects resources when running
inside the container.
• Example:
• @Inject private MyService service;
• [Link](); // Runs inside real EE context.
Summary

• Arquillian simplifies Java EE testing with container


integration.
• You can perform real environment testing using JUnit.
• Ensures higher reliability, accurate dependency injection,
and resource availability.
• Next step: Run and verify tests in Payara or WildFly
server.

You might also like