Document Version 1.0
Copyright © 2012 beijing.beijing.012@gmail.com
Keywords:
JEE integration test, JEE unit test, arquillian sample, JBoss 5, JBoss 7, EJB test, arquillian dependencies, arquillian enbedded container, managed container, remote container
Table of Contents
1. Create a simple Maven project in Eclipse
2. Create project's pom.xml file
3. Implement source code and test code
3.1 HalloEjbLocal.java
3.2 HalloEjb.java
3.3 TestHalloEJB.java
4. Download JBossjboss-as-7.1.1.Final
5. Run the test
6. Configuration Arquillian to find the JBoss Server
W ith Arquillian becoming more and more known to the JEE world, more and more people may have tried to evaluate it, to find if it could be the EJB testing tool for the next project. But it often happens, after hours of trying, we still have no luck making even a Hello World test to work. We might have problem finding all the necessary dependencies, we might miss correct configuration files, we might have written the test classes slightly different from what was shown in the official guids...
All these could happen, if you are not using the "standard combinations" shown in the official Arquillian documents. For example, currently the combination of "arquillian 1.0.0.3Final + jboss7 embedded + maven + JUnit" is quite well documented. But, if you have jboss7 managed or remote container, even jboss5 container, if your prefere TestNG over JUnit, or you are using ANT not maven, in such cases, the way to Arquillin Hello world are often not so straightforward, and you might think of giving it up.
- Tutorial EJB3 Integration Test with Arquillian part1 - JBoss 7 managed container
- Tutorial EJB3 Integration Test with Arquillian part2 - JBoss 7 remote container
- Tutorial EJB3 Integration Test with Arquillian part3 - JBoss 5 managed container
- Tutorial EJB3 Integration Test with Arquillian part4 - JBoss 5 managed container and TestNG (Testing of JPA/Entity Bean using Arquillian is also included in this part)
- Tutorial EJB3 Integration Test with Arquillian part5 - JBoss 5 managed container, TestNG and ANT(+ Ivy)
To follow this "Tutorial EJB3 Integration Test with Arquillian" serial, you should have at least of basic knowledges of :
Using Eclipse and Maven
JEE/EJB3 programming model
JUnit
Now let's start with the part1
Tutorial EJB3 Integration Test with Arquillian part1 - JBoss 7 managed container
In this section we will show an integration test of EJB3 stateless session bean using Arquillian with managed JBoss 7 container.
1. Create a simple Maven project in Eclipse
We assume you have Eclipse JEE IDE with Maven plugin. Follow the steps below to create a simple Maven project in Eclipse.
step1. Eclipse -> new Project -> Maven Project:
select check-box "Create a simple project" and "Use default Workspace location". Click "Next".
step2. in the "Maven new Project" view, provide "testarq" as Group Id and "testarq_jb7_managed" as Artifact Id.
Give "0.0.1" as Version, leave default "jar" as Packaging (type).
Click "finish".
A new Maven project called "testarq_jb7_managed" is created, and show in Eclipse:
2. Create project's pom.xml file
Replace the content of pom.xml file with the following content:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testarg</groupId>
<artifactId>testarq_jb7_managed</artifactId>
<version>0.0.1</version>
<description>test arr</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.3.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>arquillian-jbossas-managed</id>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-arquillian-container-managed</artifactId>
<version>7.1.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
For the example to work, please make sure you have arquillian-bom version "1.0.0.Final" and "jboss-as-arquillian-container-managed" with version "7.1.1.Final".
Correct arquillian-bom version helps to resolve the needed dependencies. When you have problem like "ClassNotFoundException" or ".. one of its dependencies could not be resolved: Failed to read artifact ..."
You have probabily problem with arquillian-bom version (Can also be that the dependencies are not in the (default) maven central repository, then you need to configure maven to use another repository, we will talk about this issue in detail in another post). Try to use a correct arquillian-bom, instead of adding dependencies manually.
3. Implement source code and test code
Now we ready to write the EJB and test codes.
We are going to write a HalloEJB stateless session bean. This bean has one method "sayHello(txt)", which return "Hallo xxx" text.
The bean itself consistes of 2 java classes. i.e. HalloEjb.java and HalloEjbLocal.java. We also need a test class TestHelloEJB.java
3.1 HalloEjbLocal.java
package test.ejb;
import javax.ejb.Local;
@Local
public interface HalloEjbLocal {
public String sayHello(String name);
}
3.2 HalloEjb.java
package test.ejb;
import javax.ejb.Stateless;
@Stateless
public class HalloEjb implements HalloEjbLocal {
public String sayHello(String name) {
return "Hallo " + name;
}
}
3.3 TestHalloEJB.java
package test.ejb;
import javax.ejb.EJB;
import junit.framework.Assert;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(Arquillian.class)
public class TestHalloEJB {
@Deployment
public static JavaArchive createDeployment(){
JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "halloEjb.jar");
jar.addClass(HalloEjbLocal.class).addClass(HalloEjb.class);
jar.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
//jar.addAsManifestResource("ejb-jar.xml", "ejb-jar.xml");
System.out.println(jar.toString(true));
return jar;
}
@EJB
HalloEjbLocal halloEjb;
@Test
public void sayHallo(){
String result = halloEjb.sayHello("hehe");
Assert.assertEquals("Hallo hehe", result);
}
}
The @Deployment annotation in the above tells Arquillian at runntimeto assemble a "halloEjb.jar" and deplyoment this to the assigned container.
The line "jar.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");" is very important here. Other wise, the deployment will fail.(We will see in subsequent post, that the "bean.xml" is sometimes not needed, if the used dependency version is diffeeren than Arquillian 1.0.0.Final + jboss-as-arquillian-container-managed 7.1.1.Final ).
The way EJBs are referenced here conforms with EJB3 annotation style @EJB, just like EJB references in normal EJB3 conponents. No JNDI lookup code is necessary in test code, and that is the magic of Arquillian!
When you have followed the steps till now, you would have an Eclipse project as shown in the following image:
As mentioned, we will run the EJB test in a "managed jboss7 conteiner". This means, at run time Arquillian will start an JBoss7 container, deploy the to be tested code in the JBoss, execute the test, and then shutdown the JBoss7 instance.
So we still need a JBoss7 server.
4. Download JBossjboss-as-7.1.1.FinalDowload the JBoss server from www.jboss.org, and extract it to your favorite location.
On my machine , I extracted jboss to:
/home/he/jboss-as-7.1.1.Final
In order to run jboss you still need to have add "bin" directory of jdk7 to the PATH of your system, and set JAVA_HOME to the jdk7 installation location.
Now we are "ready" to run the "TestHalloEJB" test
5. Run the test
Create an Eclipse Run configuration for maven projects as shown below:
Make sure you have used the "arquillian-jbossas-managed" profile, and Click run.
You could also run test on commandline using following command:
> mvn clean test -Parquillian-jbossas-managed
When you have correctly followed the step 1-5 till now you would see following screen in your console:
We have test error!
When you have a close look at the error text, you will see, Arquillian complains aobut missing "jbossHome". So, this is the problem.
We need to tell Arquillian where is the JBosss 7 server.
6. Configuration Arquillian to find the JBoss Server
Configuring Arquillian to find JBoss could be down in 2 ways. You could either set system property "JBOSS_HOME", or use "arquillian.xml". Using "arquillian.xml" file will provide us more felxibility, so we will take this option.
Create an "arquillian.xml" in "/src/test/resources/" folder with following content:
<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">
<container qualifier="jbossas-7-managed" default="true">
<configuration>
<property name="jbossHome">/home/he/jboss-as-7.1.1.Final</property>
</configuration>
</container>
</arquillian>
Please remember to replace "/home/he/jboss-as-7.1.1.Final" with the real JBoss7 location on your system
Now try to runt the test again. When you have not made any mistake following the above steps, you will see that the test are successful in your console:
-------------------------------------------------------
halloEjb.jar:
/META-INF/
/META-INF/beans.xml
/test/
/test/ejb/
/test/ejb/HalloEjb.class
/test/ejb/HalloEjbLocal.class
...
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:43.141s
[INFO] Finished at: Sun Dec 16 21:12:17 CET 2012
[INFO] Final Memory: 23M/186M
[INFO] ------------------------------------------------------------------------
T E S T S
-------------------------------------------------------
Forking command line: /bin/sh -c cd /home/he/workspace_arqtest/testarq_jb7_managed && /usr/java/jdk1.6.0_32/jre/bin/java -jar /home/he/workspace_arqtest/testarq_jb7_managed/target/surefire/surefirebooter7540012828763289102.jar /home/he/workspace_arqtest/testarq_jb7_managed/target/surefire/surefire6295545167876887576tmp /home/he/workspace_arqtest/testarq_jb7_managed/target/surefire/surefire_07068526860728801090tmp
Running test.ejb.TestHalloEJB
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
16.12.2012 21:11:08 org.jboss.as.arquillian.container.managed.ManagedDeployableContainer startInternal
INFO: Starting container with: [/var/jdk7/bin/java, -Xmx512m, -XX:MaxPermSize=128m, -ea, -Djboss.home.dir=/home/he/jboss-as-7.1.1.Final, -Dorg.jboss.boot.log.file=/home/he/jboss-as-7.1.1.Final/standalone/log/boot.log, -Dlogging.configuration=file:/home/he/jboss-as-7.1.1.Final/standalone/configuration/logging.properties, -Djboss.modules.dir=/home/he/jboss-as-7.1.1.Final/modules, -Djboss.bundles.dir=/home/he/jboss-as-7.1.1.Final/bundles, -jar, /home/he/jboss-as-7.1.1.Final/jboss-modules.jar, -mp, /home/he/jboss-as-7.1.1.Final/modules, -jaxpmodule, javax.xml.jaxp-provider, org.jboss.as.standalone, -server-config, standalone.xml]
21:11:17,820 INFO [org.jboss.modules] JBoss Modules version 1.1.1.GA
....
21:12:03,669 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 50871ms - Started 187 of 264 services (76 services are passive or on-demand)halloEjb.jar:
/META-INF/
/META-INF/beans.xml
/test/
/test/ejb/
/test/ejb/HalloEjb.class
/test/ejb/HalloEjbLocal.class
...
21:12:12,472 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015876: Starting deployment of "halloEjb.jar"
21:12:12,643 INFO [org.jboss.weld.deployer] (MSC service thread 1-4) JBAS016002: Processing weld deployment halloEjb.jar
21:12:12,689 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-4) JNDI bindings for session bean named HalloEjb in deployment unit deployment "halloEjb.jar" are as follows:
java:global/halloEjb/HalloEjb!test.ejb.HalloEjbLocal
java:app/halloEjb/HalloEjb!test.ejb.HalloEjbLocal
java:module/HalloEjb!test.ejb.HalloEjbLocal
java:global/halloEjb/HalloEjb
java:app/halloEjb/HalloEjb
java:module/HalloEjb
21:12:12,882 INFO [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016005: Starting Services for CDI deployment: halloEjb.jar
....
Results :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:43.141s
[INFO] Finished at: Sun Dec 16 21:12:17 CET 2012
[INFO] Final Memory: 23M/186M
[INFO] ------------------------------------------------------------------------
Congratulations!
You have successfuly tested a stateless session bean using Arquillian with managed JBoss 7.1.1.GA container
Great Article
ReplyDeleteJava Training in Chennai | Java EE online training
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteAWS Training in Bangalore
AWS training in sholinganallur
AWS training in Tambaram
AWS training in Velachery
Great code.It is working for me.Keep coding.Thank you.
ReplyDeleteJava training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
Mmorpg oyunları
ReplyDeleteinstagram takipçi satın al
tiktok jeton hilesi
TİKTOK JETON HİLESİ
antalya saç ekimi
referans kimliği nedir
instagram takipçi satın al
mt2 pvp serverler
instagram takipçi satın al
yeni perde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
Ask Romanlari
smm panel
ReplyDeletesmm panel
iş ilanları
instagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi