播放文檔說「內存數據庫中的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