4
votes

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.

2

2 Answers

2
votes

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

1
votes

You can use a master test application context file that just includes a specific context file by using Maven resource filtering

e.g.

@ContextConfiguration("classpath:test-context.xml")

where src/main/resources/test-context.xml is:

<beans>
    <import resource="${project.test.context}" />
</beans>

Then run mvn test -Dproject.test.context=context1.xml, mvn test -Dproject.test.context=context2.xml etc.

If you do that, you should also set a default maven property project.test.context in your POM.

By the way, if these are integration tests, they should by convention be called ...IT.java rather than ...Test.java, and should be run by failsafe (using mvn verify), not surefire.