cucumber java 搭建_java – 如何快速为Cucumber-jvm创建测试数据库?

我正在使用cucumber-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,但无济于事).还有其他选择吗?

最佳答案 我做的!

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值