[英]Running Junit test cases using 2 separate spring Application Context
I have a set of integration JUnit test cases that I want to run under 2 or more separate spring application contexts. 我有一组集成JUnit测试用例,我想在2个或更多单独的spring应用程序上下文中运行。 Application contexts differ in configuration settings and bean wirings. 应用程序上下文在配置设置和bean接线方面有所不同。 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. 但是,如果我使用JUnit类顶部的@ContextConfiguration批注指定应用程序上下文文件名,那么我只能为指定的应用程序上下文运行一次这些测试用例。 Is it possible to run the same JUnit test cases with different application contexts? 是否可以使用不同的应用程序上下文运行相同的JUnit测试用例?
Also, I am interested to execute the test cases once for each application context in the same test run - mvn test. 此外,我有兴趣在同一测试运行中为每个应用程序上下文执行一次测试用例 - mvn测试。
Put your test code in an abstract class and use subclasses with different @ContextConfigurations. 将测试代码放在抽象类中,并使用具有不同@ContextConfigurations的子类。 See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testing-examples-petclinic 见http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testing-examples-petclinic
You can use a master test application context file that just includes a specific context file by using Maven resource filtering 您可以使用Maven资源过滤来使用仅包含特定上下文文件的主测试应用程序上下文文件
eg 例如
@ContextConfiguration("classpath:test-context.xml")
where src/main/resources/test-context.xml
is: 其中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. 然后运行mvn test -Dproject.test.context=context1.xml
, mvn test -Dproject.test.context=context2.xml
等。
If you do that, you should also set a default maven property project.test.context
in your POM. 如果这样做,您还应该在POM中设置默认的maven属性project.test.context
。
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. 顺便说一句,如果这些是集成测试,它们按惯例应该被称为... IT.java而不是... Test.java,并且应该由failsafe运行(使用mvn verify
),而不是万无一失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.