黄瓜步骤上的 Spring Boot 加载 SQL

SpringBoot load SQL on cucumber step

我在 Integration/Functional 测试中使用 SpringBoot、Cucumber 和 RestAssured,问题是 @Sql 在 @Given 注释上不起作用。有没有办法在步骤之间执行SQL?

这是我的 MainDef

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

步骤如下:

public class UserSteps extends MainDef {

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

    }
...

这是亚军

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

我最终明确地执行了脚本:

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 的另一种变体 :

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));
    }