bretter
bretter

Reputation: 451

maven spring test application contexts

I want to use the applicationContext.xml in my src/main/resources directory from within my test harness in src/test/java. How do I load it? I have tried:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestService {
...
}

but get a file not found error. I'm using Maven and Spring. Thanks.

Upvotes: 1

Views: 2783

Answers (4)

Christopher Yang
Christopher Yang

Reputation: 3869

Here's how I do it, it may or may not be the best way for you. The main thing is it works in both Eclipse and Maven:

  • Keep exactly one copy of each applicationContext-xxx.xml file per project. NEVER copy-and-paste them or their contents, it'll create a maintenance nightmare.

  • Externalize all environmental settings to properties files (e.g. database-prod.properties and database-test.properties) and place them in src/main/resources and src/test/resources respectively. Add this line to your app contexts:

    <context:property-placeholder location="classpath:**/*.properties"/>

  • Create a superclass for all test classes and annotate it with a context configuration:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@Ignore
public class SpringEnabledTest {

  // Inheritable logger
  protected Logger logger = LoggerFactory.getLogger(this.getClass());

}
  • Add <additionalClasspathElements> to your maven-surefire-plugin configuration to make sure surefire picks up appContext and the right properties files. I've created an example here.

  • Add the location(s) of the app context files and src/test/resources to your Eclipse classpath so you can execute unit tests in Eclipse as well.

  • NEVER add src/main/resources to your Eclipse classpath, it's only a convenient place for Maven to package additional source files, it should have no bearing on Eclipse. I often leave this directory blank and create additional folders (e.g. env/DEV, env/UAT and env/PROD) outside of the src/ folder and pass a parameter to the build server and let it know from which folder it needs to copy files to src/main/resources.

Upvotes: 1

Jukka
Jukka

Reputation: 4663

Try this (note the asterisk):

@ContextConfiguration("classpath*:applicationContext.xml")

Upvotes: 3

Marcel St&#246;r
Marcel St&#246;r

Reputation: 23525

The Maven test classpath uses the files in target/test-classes. That folder contains Java classes from src/test/java and resources from src/test/resources.

The way to go is to create a test specific app context and store it under src/main/resources.

You may try to reference the file directly using file: i.e. something like file:src/main/resources/applicationContext.xml but to me this is an ugly hack.

Also, you can of course use the Maven resources plugin to copy applicationContext.xml prior to test execution.

Upvotes: 2

CodeChimp
CodeChimp

Reputation: 8154

Add the src folder to the classpath of your testing tool. If it's in Eclipse, I think you can do it from the project properties. You may have to change it to classpath:**/applicationContext.xml as well.

Upvotes: 0

Related Questions