6. Unit Test Beans from Application Context

There are two types of testing that developers typically perform before they release code into production. Functional testing is the testing you do when your application is mostly built and you want to make sure that everything works according to the functional requirements you were given.

Unit testing is the process by which you test each component of your application in isolation. Unit testing can be done far in advance of functional testing and code release. EJBs are difficult to unit test because it is cumbersome to tease them apart from the container for testing. Spring makes unit testing easy because each component is intended to be an entity unto itself – callable from anywhere.

Unit testing is an important part of the development process, so important in fact that some methodologies, including Agile [6] , require that the unit test be written before the class that it will be testing. Certainly writing the unit test first will give you a very clear idea of the use case for your class file. But the real value in unit testing is in giving you the ability to isolate where the problems may lie in your code. This will shorten the amount of time you would need to debug your code and ultimately lead to faster time to production.

As you create your unit tests, you may organize them into test suites. There are several third party and open source products that can help you set up and conduct your unit tests. JUnit (http://www.junit.org) is the most popular unit testing framework and can be integrated with Ant, Maven, the Eclipse IDE, and others. Maven (http://maven.apache.org) is a build framework used for compiling, assembling jars, and running unit tests. All of the examples that accompany this book can be built and tested using Maven, and Eclipse plugins for Junit allow you to run your unit tests from within the IDE.

Spring also has unit testing support and it can reduce the size of your test classes if you can use them. Most unit tests are annotated with @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration. The @ContextConfiguration default to loading an XML configuration file from the package and name of the test plus '-context.xml'. For org.springbyexample.di.xml.SetterMessageTest, the default XML file is org/springbyexample/di/xml/SetterMessageTest-context.xml. Specific XML configuration files can be set on the annotation if the default isn't acceptable.

Example 5. SetterMessageTest

The SetterMessageTest will use the Spring XML configuration file org/springbyexample/di/xml/SetterMessageTest-context.xml. The testMessage() method uses the SetterMessage injected by the test framework into the message field. The bean from the XML configuration will be injected into this field because of the @Autowired annotation which indicates the field should be autowired by type. Finally, the testMessage() method uses JUnit's assertNotNull and assertEquals methods to check if the SetterMessageTest instance isn't null, the message isn't null, and the message is the expected value. For assertEquals the first parameter is the error message that will be shown if the test fails. The second parameter is the expected value and third parameter is the value from the message bean.

                
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SetterMessageTest {

    final Logger logger = LoggerFactory.getLogger(SetterMessageTest.class);

    @Autowired
    private SetterMessage message = null;

    /**
     * Tests message.
     */
    @Test
    public void testMessage() {   
        assertNotNull("Constructor message instance is null.", message);
        
        String msg = message.getMessage();
        
        assertNotNull("Message is null.", msg);
        
        String expectedMessage = "Spring is fun.";
        
        assertEquals("Message should be '" + expectedMessage + "'.", expectedMessage, msg);

        logger.info("message='{}'", msg);
    }
    
}
                
            

SetterMessageTest-context.xml
                
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="message"
          class="org.springbyexample.di.xml.SetterMessage">
        <property name="message" value="Spring is fun." />
    </bean>

</beans>
                
            


[6] A conceptual framework that promotes development iterations throughout the lifecycle of a project.