by BehindJava

What is the best way of writing an integration test in Spring boot

Home » springboot » What is the best way of writing an integration test in Spring boot

In this tutorial we are going to understand about integration test in Spring boot with an example.

Basic template for writing the integration tests in springboot. the sql group is additional annotation. When ever you want to execute the particular sql queries before and after method run so u can use @SqlGroup annotation.

@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 {}

The IntegrationTest was deprecated sine spring boot 1.4, so the suggestion is using SpringBootTest after 1.4

Deprecated as of 1.4 in favor of org.springframework.boot.test.context.SpringBootTest with webEnvironment=RANDOMPORT or webEnvironment=DEFINEDPORT.