简体   繁体   English

使用2个单独的spring应用程序上下文运行Junit测试用例

[英]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.xmlmvn 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.

相关问题 Spring JUnit 测试未加载完整的应用程序上下文 - Spring JUnit Test not loading full Application Context 为具有自动装配组件的Spring Boot Service应用程序编写JUnit测试用例 - Writing JUnit test cases for a Spring Boot Service Application with autowired components 使用Maven运行时,JUnit测试用例失败,但可与Eclipse一起使用 - JUnit Test cases failing while running using Maven but works with Eclipse Junit 使用 Spring 引导进行保存和更新的测试用例 REST - Junit Test cases for Save and Update using Spring Boot REST 在 JUnit 测试类中使用属性值而不加载整个 Spring 引导应用程序上下文 - Using property values in JUnit test classes without load whole Spring Boot application context Spring Test / JUnit问题-无法加载应用程序上下文 - Spring Test / JUnit problem - unable to load application context 跨junit测试类重用spring应用程序上下文 - Reuse spring application context across junit test classes 在JUnit测试中将Maven属性传递给Spring Application上下文文件 - Passing Maven properties to Spring Application context files in JUnit test 在JUnit测试案例中触发Spring批处理作业 - Triggering Spring batch jobs in JUnit test cases 使用程序化TestNG运行Spring上下文测试 - Running a Spring context test using programatic TestNG
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM