Mike Summers
Mike Summers

Reputation: 2239

Normalizing Spring Resource for use with JUnit & war?

I'm probably thinking about this incorrectly, but here's what I'm up against:

I'm injecting Spring Resource objects into my app that give me the location of security certificates, for example:<property name="certificateResource" value="SomeCert.p12" /> where certificateResource is of type org.springframework.core.io.Resource

Running under JUnit the Resource is a classpath resource and everything is fine. When deployed as a war under Tomcat the the Resource is in a Servlet Context and requires WEB-INF/classes/ prepended to the certificate.

I've tried a number of Resource prefix and wildcard combinations but can't come-up with a single string that satisfies both contexts. So far the "solution" is to override the bean definition in src/test/resources/test-applicationContext.xml but that means maintaining the strings in two locations.

Any pointers to better solutions would be appreciated, thanks.

Upvotes: 1

Views: 169

Answers (1)

Ivan Sopov
Ivan Sopov

Reputation: 2370

I have tried to make a small but descriptive application using spring-test and spring-webmvc and using resources referenced in xml configs while these xml's are reused by production and testing spring configurations. Here is what I recieved: github.com/isopov/spring-resource-test

The central is the referencing of the prodcution xml config from the testing config:

<import resource="file:src/main/webapp/WEB-INF/applicationContext.xml" />

it is also possible to not import one xml from the other, but give each test several configs:

@ContextConfiguration(locations = { "classpath:test-applicationContext.xml",
                       "file:src/main/webapp/WEB-INF/applicationContext.xml" })

the resource itself resides in src/main/resources (I assumed you are using Maven or something derived from the "Standard Directory Layout") so is always accessible with "classpath:hello.txt" from any spring config.

I tested this with maven build, as web-application and running UTs inside Eclipse.

Upvotes: 2

Related Questions