【问题标题】:SpringBoot @IntegrationTest annotation for testingSpringBoot @IntegrationTest 注解进行测试
【发布时间】:2017-02-05 22:29:22
【问题描述】:

我对 SpringBoot 很陌生。我需要了解如何使用 SpringBoot 编写集成测试。我在 Internet 上看到了一些使用 @IntegrationTest 注释的示例,而其他一些使用 @SpringBootTest 注释的示例。

我只是想知道两者之间有什么区别?

在 Spring boot 中编写集成测试的最佳方式是什么?

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    IntegrationTest 已弃用 sine spring boot 1.4,因此建议在 1.4 之后使用 SpringBootTest

    从 1.4 开始弃用,取而代之的是使用 webEnvironment=RANDOM_PORT 或 webEnvironment=DEFINED_PORT 的 org.springframework.boot.test.context.SpringBootTest。

    【讨论】:

    • 注意:@IntegrationTest 在 Spring Boot 2.0.0 中被删除
    【解决方案2】:

    在 springboot 中编写集成测试的基本模板。 sql 组是附加注释。当您想在方法运行之前和之后执行特定的 sql 查询时,您可以使用 @SqlGroup 注释。

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = MainApplication.class,
                webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @SqlGroup({
        @Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, 
             scripts = "classpath:beforeTestRun.sql"),
        @Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, 
             scripts = "classpath:afterTestRun.sql")})
    
    public class CustomerControllerIntTest {}
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 2020-01-17
      • 2011-07-26
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多