SpringBoot在黄瓜步骤上加载SQL

时间:2017-03-10 06:05:07

标签: spring-boot integration-testing cucumber-jvm cucumber-junit cucumber-java

我使用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
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

@luboskrnac的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));
    }