Document Version 1.0
Copyright © 2012 firstname.lastname@example.org
JEE integration test, JEE unit test, arquillian sample, JBoss 5, JBoss 7, EJB test, arquillian dependencies, arquillian enbedded container, managed container, remote container
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 126.96.36.199Final + 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
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).
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:
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
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.Final
Dowload the JBoss server from www.jboss.org, and extract it to your favorite location.
On my machine , I extracted jboss to:
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:
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:
You have successfuly tested a stateless session bean using Arquillian with managed JBoss 7.1.1.GA container