What is best practice with spring-bassed webapps, junit and gradle?

Hi All I have a spring-based webapp, which has an main application context (application.xml) and several controller contexts (-servlet.xml) places in webapps/WEB-INF. I want to test my spring beans and controllers using junit, but the spring xml files will not be part of the classpath when running junit from gradle, since they are not located in /test/resource.

I could add webapp/WEB-INF to gradles sourceSets.test.runtimeClasspath, but I was wondering if there was different/better solution that I’m not aware of. /Klaus

The typical location you’d put the application context files would be src/main/resources as you’d need these files when packaging your WAR file. If you have them in that location would also make sure that they are automatically part of your test classpath. Is there a compelling reason not to have them in that location? Alternatively, you could configure the processResources task to copy specific files, you need on your classpath like you application context files. As mentioned in your post, you could also at them to the runtimeClasspath of your test source set.

Hi Benjamin

The problem is that I don’t want the spring configurations to be part of the classpath. Most of the spring apps I have seen, including http://www.springbyexample.org/examples/simple-spring-web-flow-webapp.html, have the spring files directly in WEB-INF, or a subfolder WEB-INF/spring. If you use Springs dispatch servlet named bla (defined in web.xml), then the definitions is found in WEB-INF/bla-servlet.xml.

Also it is considdered good practice to “hide” your JSP files inside WEB-INF, when using spring MVC.

for now the following works fine test {

output.classesDir = ‘build/junit-classes’



The spring-servlet.xml usually sits somewhere under WEB-INF. That’s correct. I’d expect the bean application contexts to sit under src/main/resources. Either way for testing Spring supports annotations to pull in the application contexts: @ContextConfiguration. I’d rather look into using that as it is directly supported by Spring. Have look at the Spring documentation about its testing framework.