Common Errors in SpringUnit Tests

Incorrect Name of Value in XML Configuration File


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.




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.


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.


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");
    public SpringUnitContext getIncorrectNameOfValueInXmlTest() {
        return incorrectNameOfValueInXmlTest;

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

    private SpringUnitContext incorrectNameOfValueInXmlTest;

XML Configuration File:

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