执行'play test'时,有没有办法传入系统属性以便在测试用例中使用?
String testDB = System.getProperties().getProperty("testDB");
Map<String, String> conf = new HashMap<String, String>();
if (testDB.equals("localdb")) {
conf.put("db.default.driver", "org.postgresql.Driver");
conf.put("db.default.url", "postgres://postgres:postgres@localhost/db");
} else if (testDB.equals("memorydb")) {
conf.put("db.default.driver", "org.h2.Driver");
conf.put("db.default.url", "jdbc:h2:mem:play-test");
} else {
conf.put("db.default.driver", "org.postgresql.Driver");
conf.put("db.default.url", "postgres://postgres:postgres@localhost/db");
}
running(fakeApplication(conf), new Runnable() {
public void run() {
int preRowCount = Info.find.findRowCount();
Info info = new Info("key");
info.save();
assertThat(Info.find.findRowCount()).isEqualTo(preRowCount+1);
info.delete();
}
});
我试过'play test -DtestDB = localdb',但在testDB中得到了null值。
答案 0 :(得分:6)
尝试play run -DtestDB=localdb --%test
我发现在http://www.playframework.org/documentation/1.2.4/ids的底部,它指出play test
等同于play run --%test
。
尝试play -DtestDB=localdb test
系统属性需要在命令之前。