Common Errors in SpringUnit Tests

Incorrect Name of Value in XML Configuration File

Description

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

Symptoms

junit.framework.AssertionFailedError
    at junit.framework.Assert.fail(Assert.java:47)
    ...

Cause

This error occurs when the name of a value referenced in 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 lead to unexpected null values, or in NullPointerExceptions.

Remedy

If the name of the value in the XML configuration is misspelled, then correct it. If it is missing altogether, then add an entry for it in the map associated with the test 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 IncorrectNameOfValueInXmlTest extends SpringUnitTest {
    
    public void testOne() throws Exception {
        String str = getObject("str");
        String expected = getObject("expected");
        assertTrue(expected.equals(str));
    }
    
    public SpringUnitContext getIncorrectNameOfValueInXmlTest() {
        return incorrectNameOfValueInXmlTest;
    }

    public void setIncorrectNameOfValueInXmlTest(
            SpringUnitContext incorrectNameOfValueInXmlTest) {
        this.incorrectNameOfValueInXmlTest = incorrectNameOfValueInXmlTest;
    }

    private SpringUnitContext incorrectNameOfValueInXmlTest;

}
XML Configuration File:
<beans>

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