Spring – Running Junit test cases using 2 separate spring Application Context

junit4spring

I have a set of integration JUnit test cases that I want to run under 2 or more separate spring application contexts. Application contexts differ in configuration settings and bean wirings. However, if I specify the application context file name using the @ContextConfiguration annotation at the top of the JUnit classes then I am only able to run these test cases once for the specified application context. Is it possible to run the same JUnit test cases with different application contexts?

Also, I am interested to execute the test cases once for each application context in the same test run – mvn test.

Best Answer

Put your test code in an abstract class and use subclasses with different @ContextConfigurations. See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testing-examples-petclinic