Have you been struggling with setting up integration tests for DAOs? We all need to test them, luckily there is a tool helping you to do that and that is Arquillian.
Arquillian is a container-oriented integration and functional test platform that support different types of containers where our beans life cycle are handled. It is focused to make integration tests just as easy as JUnit tests. To mention a couple of containers supported out of the box, JBoss, Jetty, Tomcat and WebSphere. Arquillian is rather new and had its first Final(1.0.0.Final) release in April this year.
In this example we will show how a test towards a JBoss container is implemented both with CDI(Contexts and Dependency Injection, integrated out of the box in the Java EE platform) and Spring dependency injection. Arquillian also have support for testing multiple containers in the same test if you for instance are using different application servers for your application.
In this example I have used:
JBoss AS7.1.1 final
Setting up the test to work with CDI
We have a class CarDao.java that we want to test as ‘production like’ as possible. In JUnit we would have to mock a lot of stuff but Arquillian will handle this for us.
The dependencies in the pom.xml to make Arquillian work are:
Arquillian core <version> 1.0.3.Final
arquillian-bom <version> 1.0.3.Final
jboss-javaee-6.0 <version> 3.0.1.Final
jboss-as-arquillian-container-remote <version> 7.1.1.Final
JUnit <version> 4.10
Now we have all plugins and settings that we need. Run a maven install to retrieve all dependencies and to setup the Arquillian profile.
Let’s continue with creating our test class to test our DAO with Arquillian, notice the annotations @RunWith and @Deployment that are Arquillian specific. Under the @Deployment annotation we specify everything that we need to run in our test case. We need CarPoolDao that we are going to inject and test, CarPool will be used in order to create a new CarPool for the CarPoolDao. For CarPool we will add the whole package if we want to add some testing for other classes later on. Also beans.xml and persistance.xml needs to be added as resources in our testclass to enable persistance and CDI.
Now, make sure that your JBoss server is running so that Arquillian can deploy the temporary container used for this test. Then run the test, it is as simple as that! No more time consuming mocking, Arquillian will handle this for you. The dependency injection will also automatically be handled by Arquillian since you are adding bean.xml as a resource in the deployed container.
Setting up the test to work with Spring
Arquillian does not have out of the box Spring support but is available in an external component ‘Arquillian Spring Framework Extension’ where @Autowire is used to inject the object under test.
In order to make it work you need to add a spring property to the arquillian.xml as follows:
[gist id=4114996 file=arquillian.xml /]
By specifying the extension spring-deployer, Arquillian will by default package the complete spring-context and deploy it temporarily for the test.
Optional(Not used in this example): You can also if you would like add the dependencies your self, by adding this property to arquillian.xml
[gist id=4115002 file=arquillian.xml /]
In the test class you will have to change the annotation @Inject to @Autowired since we are using spring injection. Also add springs applicationcontext.xml to the ShrinkWrap create method and @SpringConfiguration annotation, in order for Arquillian to have it available for the test.
[gist id=4123986 file=CarDaoTest.java /]
So testing our Spring DAO is also fairly easy, you just need to add some more dependencies in your pom.xml, which you will find in this example pom file Arquillian Spring Framework Extension.
Arquillian is an open source test framework with a lot of container-oriented goodies. For testing DAOs and other container held objects where you normally would have to use one deployed setup and test against that, I definitely see a benefit with using Arquillian.
You will find the complete source code from my lab used in this post at github.
The Arquillian team have created a spring showcase with complete examples that you can have a look at Arquillian-showcase for Spring. For more examples and show cases for other techniques for instance jax-ws, have a look Arquillian-showcase.
For more information on Arquillian check out their website.