[英]how to quickly create a test database for Cucumber-jvm?
我正在使用Cucuming-jvm来测试我正在工作的旧系统的行为。 我必须使用Java 1.5和Hibernate 3.3,无法升级。 由于在测试过程中它将一些对象存储在数据库中,因此我创建了一个新的开发数据库。
令我困扰的是,每次重新运行测试时,我必须手动删除记录(使用sql脚本),否则它们将失败。 任何其他想要运行它们的人都必须这样做。 我想通过以下任一方法快速自动清除测试数据库:
到目前为止,我所拥有的:我正在使用Cucumber-junit插件,并且RunTests类重定向到我的测试数据库:
@RunWith(Cucumber.class)
@Cucumber.Options(
features = "test/resources/cucumber",
format = "html:target/cucumber"
)
public class RunTests {
private static Configuration configuration;
@BeforeClass
public static void preparaBase() {
// gets the mapped configuration to the db
configuration = HibernateUtil.getConfiguration();
configuration.setProperty("hibernate.connection.url", "test-db-url");
configuration.setProperty("hibernate.connection.username", "user");
configuration.setProperty("hibernate.connection.password", "pass");
// configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop");
// rebuilds the configuration using my test database
HibernateUtil.rebuildSessionFactory(configuration);
}
}
我尝试将hibernate.hbm2ddl.auto
属性与create-drop
值一起使用,并使用import.sql
文件准备数据库,但是启动测试花费了很多时间,而且似乎没有检测到我的import.sql文件。
可悲的是,使用Maven及其出色的maven-sql-plugin并不是一种选择(我建议改用Maven,但无济于事)。 还有其他选择吗?
我做的!
我这样使用了ScriptRunner类:
@RunWith(Cucumber.class)
@Cucumber.Options(
features = "test/resources/cucumber",
format = "html:target/cucumber"
)
public class RunTests {
private static Configuration configuration;
String url = "test-db-url";
String user = "user";
String pass = "pass";
@BeforeClass
public static void preparaBase() {
// gets the mapped configuration to the db
configuration = HibernateUtil.getConfiguration();
configuration.setProperty("hibernate.connection.url", url);
configuration.setProperty("hibernate.connection.username", user);
configuration.setProperty("hibernate.connection.password", pass);
// rebuilds the configuration using my test database
HibernateUtil.rebuildSessionFactory(configuration);
// executes a script stored in test/resources/cucumber
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, pass);
ScriptRunner runner = new ScriptRunner(conn, false, true);
runner.runScript(new BufferedReader(new FileReader("test/resources/cucumber/db.sql")));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.