Human Resources Example - Testing a POJO Service
3.5.2. Testing A Service After you have created your POJOs and connected them together to form services, you need to test them. JBoss Microcontainer allows unit testing individual POJOs as well as services, through the use of a MicrocontainerTest class. The org.jboss.test.kernel.junit.MicrocontainerTest class inherits from junit.framework.TestCase, setting up each test by bootstrapping JBoss Microcontainer and adding a BasicXMLDeployer. It then searches 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). Examples of these deployment descriptors can be found in the Example 3.1, “Listing of the src/test/resources Directory�. Example 3.1. Listing of the src/test/resources Directory ├── log4j.properties └── org └── jboss └── example └── service ├── HRManagerAgeBasedTestCase.xml ├── HRManagerLocationBasedTestCase.xml ├── HRManagerTestCase.xml └── util ├── AgeBasedSalaryTestCase.xml └── LocationBasedSalaryTestCase.xml The test code is located in the src/test/java directory: Example 3.2. Listing of the src/test/java Directory └── org └── jboss └── example └── service ├── HRManagerAgeBasedTestCase.java ├── HRManagerLocationBasedTestCase.java ├── HRManagerTestCase.java ├── HRManagerTest.java ├── HRManagerTestSuite.java └── util ├── AgeBasedSalaryTestCase.java ├── LocationBasedSalaryTestCase.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 HRManagerTest to perform the actual work. Also included are a couple of TestSuite classes that are used to group individual test cases together for convenience.