Lalit Goyal
Lalit Goyal

Reputation: 61

Junit class - Failed to load ApplicationContext

I am working on the Spring Framework. and made one junit class but i am not able to properly load the xml files needed to run the @Test method in junit class. In my case

Please suggest me right way to declare the xml files in

@ContextConfiguration

@ContextConfiguration( locations={ "classpath:/applicationContext.xml",
        "classpath:/applicationDatabaseContext.xml" })

Error :

Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@48fa48fa] to prepare test instance [] java.lang.IllegalStateException: Failed to load ApplicationContext

Upvotes: 4

Views: 27033

Answers (1)

millhouse
millhouse

Reputation: 10007

If you are using Maven (recommended) then placing your Spring configuration files in the standard location src/main/resources (and src/test/resources for any test-specific configuration), then during the build these files will be copied to the target/classes directory.

You can reference these in your @ContextConfiguration with simply:

@ContextConfiguration(locations = { "/applicationContext.xml",
                                    "/applicationContext-test.xml"})

If you're not using Maven, I'd still recommend using the Standard Directory Layout for source and artifacts, and making your (presumably Ant-based) build process work in a similar manner.

Upvotes: 9

Related Questions