JBoss.org Community Documentation

3.4. Testing a service

We should now have a good idea about how to create POJOs and configure them using an XML deployment descriptor so how do we go about testing them? Thankfully JBoss Microcontainer makes it extremely easy to unit test individual POJOs as well as POJOs that are wired together through the use of a MicrocontainerTest class.

The org.jboss.test.kernel.junit.MicrocontainerTest class inherits from junit.framework.TestCase and as such it sets up each test by bootstrapping JBoss Microcontainer and adding a BasicXMLDeployer. It then looks on the classpath for an XML deployment descriptor with the same name as the test class ending in .xml and residing in a directory structure representing the class's package name. Any beans found in this file are deployed and can then be accessed using a convenience method called getBean(String name).

You can see examples of these deployment descriptors in the src/test/resources directory:

org/jboss/example/service/HRManagerTestCase.xml
                         /HRManagerAgeBasedTestCase.xml
                         /HRManagerLocationBasedTestCase.xml

org/jboss/example/service/util/AgeBasedSalaryTestCase.xml
                              /LocationBasedSalaryTestCase.xml

The test code is located in the src/test/java directory:

org/jboss/example/service/HRManagerTestCase.java
                         /HRManagerAgeBasedTestCase.xml
                         /HRManagerLocationBasedTestCase.xml
                         /HRManagerTest.java
                         /HRManagerTestSuite.java

org/jboss/example/service/util/AgeBasedSalaryTestCase.java
                              /LocationBasedSalaryTestVase.java
                              /SalaryStrategyTestSuite.java

The HRManagerTest class extends MicrocontainerTest in order to set up a number of employees to use as the basis for the tests. Individual test cases then subclass this to perform the actual work. You can also see a couple of TestSuite classes that are used to group individual test cases together for convenience.

To run the tests simply enter mvn test from the humanResourcesService/ directory. You should see some DEBUG log output which shows JBoss Microcontainer booting up and deploying beans from the relevant XML file before running each test. At the end of the test it then undeploys the beans and shuts down the microcontainer.

Note

Some of the tests such as HRManagerTestCase, AgeBasedSalaryTestCase and LocationBasedSalaryTestCase simply unit test individual POJOs whilst other tests such as HRManagerAgeBasedTestCase and HRManagerLocationBasedTestCase unit test the whole service consisting of multiple POJOs wired together. Either way the method for conducting the tests remains the same. Thanks to the MicrocontainerTest class it is trivial to set up and conduct comprehensive tests for any part of your code.

Finally note that we didn't unit test the Address or Employee classes here. They were deliberately left out as they only contain trivial logic which is very unlikely to break.