Common Errors in SpringUnit Tests

Incorrect Name of XML Configuration File

Description

A SpringUnit test fails because the XML configuration file has an incorrect file name.

Symptoms

org.springframework.beans.factory.BeanDefinitionStoreException:
        IOException parsing XML document from class path resource [IncorrectXmlFilenameTest.xml];
        nested exception is java.io.FileNotFoundException:
        class path resource [IncorrectXmlFilenameTest.xml] cannot be opened because it does not exist
java.io.FileNotFoundException: class path resource [IncorrectXmlFilenameTest.xml]
        cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
    ...

Cause

This error occurs when an XML configuration file has been created and is on the classpath, but the name of the file does not conform to the SpringUnit convention.

Remedy

Ensure the name of the file is Classname.xml where Classname is equivalent to the simple name of the Java test class. For example, if your test class is com.my.company.FooTest, then the name of your XML file should be FooTest.xml.

Example

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

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

public class IncorrectXmlFilenameTest extends SpringUnitTest {
    
    public void testWillFail() throws Exception {
    }
    
    public SpringUnitContext getIncorrectXmlFilenameTest() {
        return incorrectXmlFilenameTest;
    }

    public void setIncorrectXmlFilenameTest(
            SpringUnitContext incorrectXmlFilenameTest) {
        this.incorrectXmlFilenameTest = incorrectXmlFilenameTest;
    }

    private SpringUnitContext incorrectXmlFilenameTest;

}
XML Configuration File:
!! -- File name is IncorrectXXmlFilenameTest.xml -- !!
Previous Next