This tutorial familiarquillian plug-in for JBoss Forge. After reading this tutorial, you can master:
- Install the Arquillian plug-in the Forge Environment
- Use this plug-in to add the Arquillian infrastructure to a Maven-based Java project.
- Generate components and corresponding Arquillian tests
- You do not need to manually modify to run the Arquillian test in different containers
You will complete many steps that are the same as those described in the Quick Start tutorial. The difference is that some trivial things are left to Forge for processing. This tutorial is intended for quick reading, which is to help you get started faster.
Prerequisites
This tutorial assumes that you have installed JBoss Forge. It is not difficult to install Forge. Please refer to the "quick start tutorial" or the official Forge document to download and install it. You also need to install JDK on your machine.1.6 or later.
Create a project
Before using Arquillian, we need to create a project. If you have not created a project, use the following Forge command to create a simple Java EE 6 project, including support for JPA.
$ new-project --named arquillian-demo --topLevelPackage demo
? Use [/Users/paul/arquillian-demo] as project directory? [Y/n]***SUCCESS*** Created project [arquillian-demo] in new working directory [/Users/paul/arquillian-demo]Wrote /Users/paul/arquillian-demoWrote /Users/paul/arquillian-demo/pom.xmlWrote /Users/paul/arquillian-demo/src/main/javaWrote /Users/paul/arquillian-demo/src/test/javaWrote /Users/paul/arquillian-demo/src/main/resourcesWrote /Users/paul/arquillian-demo/src/test/resourcesWrote /Users/paul/arquillian-demo/src/main/resources/META-INF/forge.xml
The new project has been created. Next, we need to add some code for testing. The following uses Forge to create a CDI Bean.
First, we use the "beans" plug-in to install CDI into the project.
$ beans setup
***SUCCESS*** Installed [forge.spec.cdi] successfully. ? Do you want to install CDI APIs? [y/N]
Wrote /Users/paul/arquillian-demo/src/main/resources/META-INF/beans.xml
Then create a Bean.
$ beans new-bean --type demo.MySimpleBean --scoped DEPENDENT
Picked up type <JavaResource>: demo.MySimpleBeanWrote /Users/paul/arquillian-demo/src/main/java/demo/MySimpleBean.java
The project is ready. perform the test below.
Getting started
Setting Arquillian is simple. You may have read it in the Quick Start tutorial. Configuring a new Arquillian container in pom. xml requires a lot of copying and pasting work. Forge can use a command to complete these tasks.
First, install the Arquillian plugin.
$ forge install-plugin arquillian
Now we need to configure Arquillian and container. JBoss AS7 is used here.
$ arquillian setup --container JBOSS_AS_7_MANAGED
Forge will prompt you to use JUnit, Arquillian, and JBoss AS7. Select the latest version. If you haven't downloaded JBoss AS7, Forge can even help you automatically complete it.
In this case, Arquillian is configured. You can view the dependenices and profile generated in the pom. xml file. Forge also creates an arquillian. xml file in src/test/resources.
Write Test
Write a test now. Use Forge again to help you.
$ arquillian create-test --class demo.MySimpleBean.java
Picked up type <JavaResource>: demo.MySimpleBeanTestWrote /Users/paul/arquillian-demo/src/test/java/demo/MySimpleBeanTest.java
A new test class containing deployment and test methods is added to the project. It uses CDI to inject the class to be tested, so that it can immediately verify whether the test can be run in the container. If imports cannot be found in your IDE, check whether the created Maven profile is enabled. You can directly run the test from IDE, Maven command line, or Forge.
$ build --profile arq-jbossas-7-managed
Congratulations! You have used Arquillian and Forge to get the first green bar!
Next we will try to test the JPA code.
Test JPA
Configure JPA before writing a test using Java Persistence API (JPA. We will create an Entity and write a simple Data Access Object. This is the class we will test.
$ persistence setup --provider HIBERNATE --container JBOSS_AS7
***SUCCESS*** Installed [forge.spec.jpa] successfully.***INFO*** Setting transaction-type="JTA"***INFO*** Using example data source [java:jboss/datasources/ExampleDS]Warning: The encoding 'UTF-8' is not supported by the Java runtime. ? The JPA provider [HIBERNATE], also supplies extended APIs. Install these as well? [y/N] [false] Wrote /Users/paul/arquillian-demo/pom.xmlWrote /Users/paul/arquillian-demo/src/main/resources/META-INF/persistence.xml
Create Entity now.
$ entity --named Language --package demo.entities
Created @Entity [demo.entities.Language]Picked up type <JavaResource>: demo.entities.LanguageWrote /Users/paul/arquillian-demo/src/main/java/demo/entities/Language.java
Then add some attributes to the Entity.
$ field string --named name
Added field to demo.entities.Language: @Column private String name;Wrote /Users/paul/arquillian-demo/src/main/java/demo/entities/Language.java
You must copy the following file to your project.
Src/main/java/demo/dao/LanguageDao. java
package demo.dao;
import demo.entities.Language;import javax.annotation.PostConstruct;import javax.ejb.Singleton;import javax.ejb.Startup;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import java.util.List;
@Singleton@Startuppublic class LanguageDao { @PersistenceContext EntityManager em;
public List<Language> listLanguages() { return em.createQuery("select l from Language l").getResultList(); }
@PostConstruct public void insertTestData() { Language java = new Language(); java.setName("Java"); em.persist(java);
Language ruby = new Language(); ruby.setName("Ruby"); em.persist(ruby);
Language groovy = new Language(); groovy.setName("Groovy"); em.persist(groovy); }}
Now, create our test class. If you have completed the first part of the tutorial, this command is similar.
$ arquillian create-test --class demo.dao.LanguageDao.java --enableJPA
Picked up type <JavaResource>: demo.dao.LanguageDemoTestWrote /Users/paul/arquillian-demo/src/test/java/demo/dao/LanguageDemoTest.java
This command creates a test class and adds persistence. xml to the test deployment. Open this test class in IDE and addLanguage
To deployment.
Now add a test assertion:
Src/test/java/demo/dao/LanguageDaoTest. java
@Testpublic void testListLanguages() {Assert.assertEquals(3, languagedao.listLanguages().size());}
Congratulations! Another Arquillian (greenbar) green bar!
Add other containers
It is easy to add other containers. Just run the setup command again. For example, add Glassfish.
$ arquillian setup --container GLASSFISH_3_1_REMOTE
To switch between containers, you only need to switch Maven profile. For example, in Forge:
$ build test --profile glassfish-remote-3
Export deployment package
It is useful to export the test to a deployment file for debugging. Forge can help you.
First go to an Arquillian test:
$ cd src/test/java/demo/MySimpleBeanTest.java
Then export the deployment file:
$ arquillian export
You can find the deployment file in the target directory of the project.
For details about Arquillian, click here
Arquillian's: click here