Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit class - Failed to load ApplicationContext

Tags:

java

junit

spring

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

  • xml file are placed under folder WEB-INF
  • the junit test class is under test/<package_name>

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

like image 221
Lalit Goyal Avatar asked Jan 19 '23 07:01

Lalit Goyal


1 Answers

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.

like image 55
millhouse Avatar answered Jan 25 '23 13:01

millhouse