Junit Class - Failed to load ApplicationContext

I am working on Spring Framework. and made one junit class, but I can’t load the xml files needed to run the @Test method in the junit class correctly. In my case

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

Please suggest me the correct way to declare xml files in

@ContextConfiguration

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

The error is :

An exception was fixed when using TestExecutionListener [Org.springframewor k.test.context.support.DependencyInjectionTestExecutionListener@ 48fa48fa] to prepare a test instance [] java.lang.IllegalStateException: ApplicationContext could not be loaded

+4
source share
1 answer

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

You can reference them in your @ContextConfiguration simply:

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

If you're not using Maven, I still recommend using the Standard Catalog Layout for the source and artifacts and make your (supposedly Ant based) build process the same way.

+9
source

Source: https://habr.com/ru/post/1380711/


All Articles