播放文档说“内存数据库中的H2对于开发非常方便,因为您的演变在重新开始播放时从头开始运行。”这就是我想要的。这里是我迄今所做的(播放2.5.6):使用H2内存数据库设置Scala Play测试
我创建使用H2与演化测试配置文件,内容如下:
play.evolutions { db.default.enabled = true autoApply = true } db { default { driver = org.h2.Driver url = "jdbc:h2:mem:test;MODE=MYSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1" username = sa password = "" pool = "bonecp" // otherwise you can't log your sql... bonecp.logStatements=true } }
我添加了一个简单演化文件“/conf/evolutions/default/1.sql”:
# --- !Ups CREATE TABLE PROJECTS ( id int(11) NOT NULL ) ; # --- !Downs DROP TABLE IF EXISTS PROJECTS;
我的控制器需要一个数据库。这是测试数据库应该被注入而不是MySQL的地方。它有一个测试的行动,读取表:
class DataManagementController @Inject()(db: Database) extends Controller { def test() = Action { db.withConnection { conn => val st = conn.createStatement() val res = st.executeQuery("SELECT * FROM PROJECTS") while (res.next()) { println(res.getInt("id")) } } Ok("") } }
,我写我的行动的第一个测试如下:
class ControllerSpec extends PlaySpec with OneAppPerSuite { val TestDb = Databases.inMemory("default") val dataCtrl = new DataManagementController(TestDb) "DataManagementController" should { "test" in { dataCtrl.test().apply(FakeRequest()) } } }
当我运行测试,我看到变阵得到应用到H2分贝,但在查询运行时,每样东西尽管
DB_CLOSE_DELAY=-1
选项删除(?):~ test-only ControllerSpec [info] ControllerSpec: [debug] c.j.b.BoneCPDataSource - JDBC URL = jdbc:h2:mem:test;MODE=MYSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1, Username = sa, partitions = 1, max (per partition) = 30, min (per partition) = 5, idle max age = 10 min, idle test period = 1 min, strategy = DEFAULT [debug] c.j.b.StatementHandle - select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%' [error] o.j.StatementLogger - java.sql.Statement.executeQuery: select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%'; throws exception: org.h2.jdbc.JdbcSQLException: Table "play_evolutions" not found; SQL statement: select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%' [42102-192] [...] [debug] c.j.b.StatementHandle - create table play_evolutions ( [...] [debug] c.j.b.StatementHandle - CREATE TABLE PROJECTS ( id int(11) NOT NULL AUTO_INCREMENT, person_id int(11) NOT NULL, name varchar(255) NOT NULL, PRIMARY KEY (id), ) [debug] c.j.b.StatementHandle - update play_evolutions set state = 'applied' where id = 1 [debug] c.j.b.StatementHandle - select id, hash, apply_script, revert_script, state, last_problem from play_evolutions where state like 'applying_%' [info] DataManagementController [info] application - Creating Pool for datasource 'default' [info] - should test *** FAILED *** [info] org.h2.jdbc.JdbcSQLException: Table "PROJECTS" not found; [...]
如果我和探索数据库(url:“jdbc:h2:mem:test”+ args),它是空的。但是,如果我现在修改“1.SQL”,我得到
Database 'default' is in an inconsistent state![An evolution has not been applied properly. Please check the problem and resolve it manually before marking it as resolved.]
,但它并没有说如何解决这个问题(因为我在浏览器中看到一个空数据库)。
我将不胜感激设置测试数据库的任何帮助。我在这里看到很多帖子提出类似的问题,无论是没有答案,也没有接受,有几种不同的方法,而且我几乎一直在努力尝试几天。注:它将在StackOverflow文档(和Play文档btw)中做出有用的部分。
也可以看看http://tour.acolyte.eu.org/ – cchantep