用于测试的SpringBoot @IntegrationTest注释

时间:2016-09-28 05:59:12

标签: spring-boot

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

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

在Spring启动中编写集成测试的最佳方法是什么?

2 个答案:

答案 0 :(得分:26)

IntegrationTest已弃用正弦弹簧启动1.4,因此建议在1.4之后使用SpringBootTest

  

从1.4开始,不赞成使用webEnvironment = RANDOM_PORT或webEnvironment = DEFINED_PORT的org.springframework.boot.test.context.SpringBootTest。

答案 1 :(得分:1)

用于在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 {}
相关问题