Go back to Index
In this session we will look into test driven development using Arquillian.
This guide is a followup on the setting-up-java-ee-6-with-maven
Lets start by adding the required dependencies for Arquillian to our project.
Open the pom.xml file, the first thing we are going to do is to upgrade the surefire plugin, this is to solve the following bug so, in the plugins section of the build tag add the following
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
</plugin>
Once this is done we are going to add the arquillian specific tags do this after the <build> section.
<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>
and we have one more dependency to add, this one will go outside of the dependency management area and in the nextcoming dependencies tag.
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
<version>1.0.0.CR3</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>1.1.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
We are now ready to test CDI, lets create some classes. The first class we will create is a UserController.java under “src/main/java/com/bekkestad/examples/tutorial” and we need a corresponding test class called UserControllerTest.java in “src/test/java/com/bekkestad/examples/tutorial”
Here is the code for the UserController.java class
package com.bekkestad.examples.tutorial;
public class UserController {
public void log(String name){
System.out.println(createLog(name));
}public String createLog(String name){
return name + ” has logged in!”;
}}
package com.bekkestad.examples.tutorial;
import org.junit.Assert;
import org.junit.Test;public class UserControllerTest {
@Test
public void should_log_user(){
Assert.fail(“Not yet implemented”);
}
}
As you can see this will not do much at the moment, but in the next few steps we will enable Arquillian.
The first thing we need to do is to tell the class it is running Arquillian and then we need to create a deployment.
1. Add @RunWith(Arquillian.class) above the class name (UserControllerTest )
2. Add a new method createDeployment, we will keep is simple now, and just return a JavaArchive, containning our UserController and a bean.xml file.
@Deployment
public static JavaArchive createDeployment(){
return ShrinkWrap.create(JavaArchive.class)
.addClass(UserController.class)
.addAsManifestResource(EmptyAsset.INSTANCE, “beans.xml”);
}
3. You can press Ctrl+Shift+O in order to fix the imports
Now we are ready to test the class. The expected result would be for our test case to fail, since we told the method it is not yet implemented.
Go ahead and run the test, by right clicking on the “UserControllerTest.java” and choose “Run As/JUnit Test” and in the JUnit new you should see a Red bar stating
“java.lang.AssertionError: Not yet implemented!”
We now know that the server is setup for testing with Arquillian and we are using Weld to handle the CDI Injection.
Let us now do something with the test, if a user logs in we expect to get the following text name + ” has logged in!”
So we need to verify that we get this text, we can do that by using
Assert.assertEquals(String expected, String actual);
To make this class more reusable, i have added a static variable with the USERNAME that i will use in all the test cases implemented in the future.
So the final code of the class should look like this.
package com.bekkestad.examples.tutorial;
import javax.inject.Inject;
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.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;@RunWith(Arquillian.class)
public class UserControllerTest {private static final String USERNAME = “TEST”;
@Inject
UserController user;@Deployment
public static JavaArchive createDeployment(){
return ShrinkWrap.create(JavaArchive.class)
.addClass(UserController.class)
.addAsManifestResource(EmptyAsset.INSTANCE, “beans.xml”);
}@Test
public void should_log_user(){
Assert.assertEquals(USERNAME + ” has logged in!”, user.createLog(USERNAME));
}
}
If you run this again you will get a green result, telling you that the test case is successful.
And this concludes this simple example of testing CDI with Arquillian.
You can get the code from GitHub
In the next demo we will go more into using different containers, testing database connections etc.
Go back to Index
