Common Errors in SpringUnit Tests

XML Configuration File Does Not Exist

Description

A SpringUnit test fails because the XML configuration file cannot be found.

Symptoms

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

Cause

This error has these likely causes:
  1. The XML configuration file exists but is not on the classpath
  2. The XML configuration file exists, is on the classpath, but in the wrong location. Specifically, it must be located either in the default package at the root of the classpath or in the same package as the corresponding Java SpringUnit test class.
  3. The XML configuration file does not exist at all
  4. The name of the XML configuration file does not observe the SpringUnit convention. See Incorrect Name of XML Configuration File.

Remedy

The remedy depends upon the cause:
  1. Move the XML configuration file to a location on the classpath
  2. Create an XML configuration file and place it in a location on the classpath
  3. Rename the XML configuration file to conform to the naming convention. See Incorrect Name of XML Configuration File.

Example

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

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

public class NoXmlFileTest extends SpringUnitTest {
    
    public void testWillFail() throws Exception {
    }
    
    public SpringUnitContext getNoXmlFileTest() {
        return noXmlFileTest;
    }

    public void setNoXmlFileTest(SpringUnitContext noXmlFileTest) {
        this.noXmlFileTest = noXmlFileTest;
    }

    private SpringUnitContext noXmlFileTest;

}
XML Configuration File:
!! -- DOESN'T EXIST IN FILE SYSTEM OR CLASSPATH -- !!
Previous Next