Search code examples
spring-bootintegration-testingcucumber-jvmcucumber-junitcucumber-java

SpringBoot load SQL on cucumber step


Im using SpringBoot, Cucumber and RestAssured for my Integration/Functional Tests, the problem is @Sql does not work on @Given annotation. Is there a way to execute SQL between steps?

Here's my MainDef

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringBootContextLoader.class)
@WebAppConfiguration
public abstract class MainDef {}

Here's the Steps:

public class UserSteps extends MainDef {

    @Given("^delete_users$")        
    @Sql("classpath:config/usersql/deleteUser.sql")
    public void delete_users() throws Throwable {

    }
...

Here's the Runner

@RunWith(Cucumber.class)
@CucumberOptions(format = "pretty", features = "src/test/resources/feature/", tags = "~@ignore",glue = {"com.user.definition"})
public class CucumberTest { //NOSONAR
}

Solution

  • I ended up executing script explicitly:

    public class UserSteps extends MainDef {
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Given("^delete_users$")        
        public void delete_users() throws Throwable {
            ScriptUtils.executeSqlScript(
                jdbcTemplate.getDataSource().getConnection(),
                new ClassPathResource("config/usersql/deleteUser.sql")
            );
        }
    }