Spring boot SpringBoot在第二步上加载SQL
我使用SpringBoot、Cucumber和Restassed进行集成/功能测试,问题是@Sql不能在@给定注释上工作。是否有一种在步骤之间执行SQL的方法 这是我的主菜Spring boot SpringBoot在第二步上加载SQL,spring-boot,integration-testing,cucumber-jvm,cucumber-junit,cucumber-java,Spring Boot,Integration Testing,Cucumber Jvm,Cucumber Junit,Cucumber Java,我使用SpringBoot、Cucumber和Restassed进行集成/功能测试,问题是@Sql不能在@给定注释上工作。是否有一种在步骤之间执行SQL的方法 这是我的主菜 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Application.class, loader = SpringBootContextLoader.class) @WebAppConfiguration public abst
@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的另一个变体:
我也提出了同样的解决方案,我想这是在步骤之间执行sql最干净的方法。谢谢
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));
}