2

I have a WAR project and I am trying to store spring configuration files within the /WebContent/WEB-INF/Spring/ folder (as required by my employer), but when I do that I have not found a way to be able to reference those files within my junit tests. I was told today that I can modify my MANIFEST.MF file so that the spring configuration files are included, but I have not been able to successfully reference the files from the manifest file.

My first question is whether or not it is possible to reference files in the /WebContent/WEB-INF/Spring/ folder from my junits? If it is possible, how to the manifest file know where to look for the files? Are the links relative?

The folder structure for my WAR is as follows (structure is defined by the company I work for):

WARProject
 - src\
 - WebContent\
 - - WEB-INF\
 - - - Spring\
 - - - - App-config.xml
 - - - - Security.xml
 - - META-INF\
 - - - MANIFEST.MF

I appreciate any guidance you can provide.

Jeremy

jwmajors81
  • 1,430
  • 4
  • 20
  • 36

2 Answers2

1

If you are using Spring 2.5 or later and Spring's TestContext framework, then you can simply annotate the test class with @ContextConfiguration(locations = {path/to/file})

If you are loading an ApplicationContext yourself, then just make sure that /WebContent/WEB-INF/Spring/ is on the classpath and load the file with classpath:contextname.xml.

matt b
  • 138,234
  • 66
  • 282
  • 345
1

In any case, regardless of version of Spring, you can use the FileSystemXmlApplicationContext class to load your application context for testing, and pass the string "file:WebContent/WEB-INF/Spring/App-config.xml" in as the 'configLocation' argument, and this will load your application context for you from the XML configuration.

With that in mind, Matt does have a good idea in that if you are capable of using annotation based testing, you should use the @ContextConfiguration annotation in conjunction with the @RunWith(SpringJUnit4TestRunner.class) annotation to have Spring's testing framework load the context for you automatically, in which case you could also use the string above in the @ContextConfiguration annotation. You don't need to have the application context in your classpath, nor do you need to add /WebContent/WEB-INF/Spring/ to your classpath just for testing.

Alex Marshall
  • 10,162
  • 15
  • 72
  • 117