3

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
}
jvaldenor
  • 67
  • 1
  • 6

2 Answers2

0

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")
        );
    }
}
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • I also come up with the same solution, I guess this is the cleanest way to execute sql between steps. Thanks – jvaldenor Mar 13 '17 at 01:20
0

Another variation of @luboskrnac's answer:

import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StreamUtils;

@Given("AD Group activations are loaded")
    public void adGroupActivationsAreLoaded() throws IOException {
        jdbcTemplate.execute(StreamUtils.copyToString(new ClassPathResource("sql/activations/adGroupActivationsAreLoaded.sql").getInputStream(), UTF_8));
    }
ranma2913
  • 1,058
  • 10
  • 8