Running Junit test cases using 2 separate spring Application Context

I have a set of JUnit integration test cases that I want to run under two or more separate spring application contexts. Application contexts vary in configuration settings and bean postings. However, if I specify the name of the application context file using the @ContextConfiguration annotation at the top of the JUnit classes, I can only run these test cases once for the specified application context. Can I run the same JUnit test cases with different application contexts?

In addition, I am interested in running test cases once for each application context in the same test run - the mvn test.

+4
source share
2 answers

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

+2
source

You can use the context file of the main test application, which includes only a specific context file, using Maven resource filtering

eg.

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

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

 <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 this, you should also set the default maven property project.test.context in your POM.

By the way, if these are integration tests, they should be conditionally called ... IT.java, not ... Test.java, and should be performed using fail-safe (using mvn verify ), and not surefire.

+1
source

Source: https://habr.com/ru/post/1342926/


All Articles