简体   繁体   中英

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

  • 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM