Java – IOException parsing XML document from class path resource – file does not exist – Spring

javajunit4mavenspringxml

I'm aware that this question about the spring xml config file not been found in execution has been asked several times before, but none of the other answers seems to work for me.

I'm trying to run some test in a Maven Project with Spring and JUnit and I get this error all the time:

T E S T S
Running es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest
12-ago-2014 13:37:33 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
12-ago-2014 13:37:33 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2f8bbc98: startup date [Tue Aug 12 13:37:33 CEST 2014]; root of context hierarchy
12-ago-2014 13:37:33 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [nosaTenda-spring-config.xml]
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.225 sec <<< FAILURE!

Results :

Tests in error:
es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest
es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest: Could not initialize class es.udc.jcastedo.NosaTenda.test.model.util.DbUtil

Tests run: 2, Failures: 0, Errors: 2, Skipped: 0

This is a link to the complete trace of the surefire report: https://dl.dropboxusercontent.com/u/2635926/es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest.txt

Where you can find this lines:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [nosaTenda-spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [nosaTenda-spring-config.xml] cannot be opened because it does not exist

Caused by: java.io.FileNotFoundException: class path resource [nosaTenda-spring-config.xml] cannot be opened because it does not exist

es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest Time elapsed: 0.003 sec <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class es.udc.jcastedo.NosaTenda.test.model.util.DbUtil
at es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest.cleanDb(ProductoServiceTest.java:52)

DbUtil is an auxiliary class called by all tests, where the context is initialized:

public class DbUtil {
    static {
        ApplicationContext context = new ClassPathXmlApplicationContext("nosaTenda-spring-config.xml");
        transactionManager = (PlatformTransactionManager) context
            .getBean("transactionManager");
        productoDao = (ProductoDao) context.getBean("productoDao");
        tiendaDao = (TiendaDao) context.getBean("tiendaDao");
    }

    ...

    public static void populateDb() {
    ...
    }
    public static void cleanDb() throws Throwable {
    ...
    }
}

Apparently there lays the problem, as it seems the xml config file is not found no matter what and it breaks at the ClassPathXmlApplicationContext method call.

Theoretically, the spring config file is located where it should be, in src/main/resources.

I have tried all combinations, from the qualified name "/NosaTenda/src/main/resources/nosaTenda-spring-config.xml" to "nosaTenda-spring-config.xml", with the same result.

I don't know what's wrong with the class path, or if the problem is elsewhere.

EDIT

Here is the pom of the project, I just remembered that I added some filtering in the build-resources section, maybe I did something wrong there and that's the problem.

https://dl.dropboxusercontent.com/u/2635926/pom.xml

Best Solution

Generally your classpath would be .../src/main/java/, and ClassPathXmlApplicationContext would read from .../src/main/resources. Then your test classes would be in .../src/test/java/ and ClassPathXmlApplicationContext would read from .../src/test/resources.

So, check where your test application context is, and put a copy of the xml there.

Related Question