Common Errors in SpringUnit Tests

Incorrect Name of Test in XML Configuration File

Description

A SpringUnit test fails because the name of a test in the Java source code cannot be found in the XML configuration file.

Symptoms

java.lang.NullPointerException
    at org.springunit.examples.errors.IncorrectNameOfTestInXmlTest.testOne(IncorrectNameOfTestInXmlTest.java:11)
    ...

Cause

This error occurs when the name of a test method in the Java source code cannot be found in the XML configuration file. The getObject method in org.springunit.framework.SpringUnitTest and org.springunit.frameowrk.SpringTransactionalTest returns null if it can find no match in the test context. This can result in downstream NullPointerExceptions.

Remedy

If the name of the test in the XML configuration is misspelled, then correct it. If it is missing altogether, then add an entry for the test and its data in the XML configuration file.

Example

Java Source Code:
package org.springunit.examples.errors;

import org.springunit.framework.SpringUnitContext;
import org.springunit.framework.SpringUnitTest;

public class IncorrectNameOfTestInXmlTest extends SpringUnitTest {
    
    public void testOne() throws Exception {
        String str = getObject("str");
        String expected = getObject("expected");
        assertTrue(expected.equals(str));
    }
    
    public SpringUnitContext getIncorrectNameOfTestInXmlTest() {
        return incorrectNameOfTestInXmlTest;
    }

    public void setIncorrectNameOfTestInXmlTest(
            SpringUnitContext incorrectNameOfTestInXmlTest) {
        this.incorrectNameOfTestInXmlTest = incorrectNameOfTestInXmlTest;
    }

    private SpringUnitContext incorrectNameOfTestInXmlTest;

}
XML Configuration File:
<beans>

    <bean id="incorrectNameOfTestInXmlTest" class="org.springunit.framework.SpringUnitContext">
        <property name="data">
            <map>
                <entry key="testTwo">
                    <map>
                        <entry key="str"><value>idea</value></entry>
                        <entry key="expected"><value>idea</value></entry>
                    </map>
                </entry>
            </map>
        </property>
    </bean>
    
</beans>
Previous Next