[英]how to quickly create a test database for Cucumber-jvm?
I am using cucumber-jvm to test the behaviour of the legacy system I'm working on at work. 我正在使用Cucuming-jvm来测试我正在工作的旧系统的行为。 I have to use Java 1.5 and Hibernate 3.3, upgrading is not an option.
我必须使用Java 1.5和Hibernate 3.3,无法升级。 Since, during my tests, it stores some objects in the database, I have created a new development database.
由于在测试过程中它将一些对象存储在数据库中,因此我创建了一个新的开发数据库。
What bothers me is that I have to manually drop the records (using a sql script) everytime I'll rerun my tests, or they'll fail. 令我困扰的是,每次重新运行测试时,我必须手动删除记录(使用sql脚本),否则它们将失败。 And anyone else wanting to run them would have to do the same.
任何其他想要运行它们的人都必须这样做。 I want to quickly and automatically clean my test database, by either:
我想通过以下任一方法快速自动清除测试数据库:
What I have so far: I'm using the cucumber-junit plugin, and the RunTests class redirects to my test database: 到目前为止,我所拥有的:我正在使用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);
}
}
I have tried using the hibernate.hbm2ddl.auto
property with the create-drop
value and using the import.sql
file to prepare the database, but it takes ages to start the tests and it seems that it's not detecting my import.sql file. 我尝试将
hibernate.hbm2ddl.auto
属性与create-drop
值一起使用,并使用import.sql
文件准备数据库,但是启动测试花费了很多时间,而且似乎没有检测到我的import.sql文件。
Sadly, using Maven and its excellent maven-sql-plugin is not an option (I have suggested a switch to Maven, to no avail). 可悲的是,使用Maven及其出色的maven-sql-plugin并不是一种选择(我建议改用Maven,但无济于事)。 Is there an alternative?
还有其他选择吗?
I did it! 我做的!
I used this ScriptRunner class as such: 我这样使用了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.