Better way to write unit tests with Spring Boot

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

See this StackOverflow answer

Leave a Reply

Your email address will not be published. Required fields are marked *