2015-02-06 38 views
8

我正在嘗試配置我的play framework應用程序,以便它在運行時使用mysql數據庫,並在內存數據庫中進行測試。 當我運行測試時,它連接到mysql數據庫而不是內存數據庫。 任何人都知道爲什麼?玩框架在內存中使用h2數據庫進行單元測試

這是我的配置:

db.default.driver=com.mysql.jdbc.Driver 
db.default.url="jdbc:mysql://localhost/communityRoots?characterEncoding=UTF-8" 
db.default.user=root 
db.default.password= "" 

db.test.driver=org.h2.Driver 
db.test.url="jdbc:h2:mem:play;MODE=MYSQL" 
db.test.user=sa 
db.test.password="" 

這是我的測試:

running(fakeApplication(inMemoryDatabase("test")), new Runnable() { 
     public void run() { 
      new User("[email protected]", "Bob", "secret").save(); 
      assertNotNull(User.authenticate("[email protected]", "secret")); 
      assertNull(User.authenticate("[email protected]", "badpassword")); 
      assertNull(User.authenticate("[email protected]", "secret")); 
     } 
    }); 
+0

另一個好方法是使用DEV H2和測試application.conf,一對在生產模式下只有一個分離應用prod.conf文件 – Loic 2015-02-08 07:30:18

+0

使用MySQL是的,我認爲這將是解決問題的方法。希望有一個更簡單/更乾淨的方式來做到這一點。謝謝 – Ciaran0 2015-02-08 09:47:51

+1

作爲一個方面說明:我使用MariaDB4j作爲嵌入式數據庫進行測試。這是一個真正的MySQL兼容數據庫,而不是H2的不完整MySQL模式。 https://github.com/vorburger/MariaDB4j – akkie 2015-02-10 07:30:28

回答

1

從實際應用中我開發:

import play.api.inject.bind 
import org.scalatest.mock.MockitoSugar 
import play.api.Application 
import play.api.inject.guice.GuiceApplicationBuilder 
import database.AccountDAO 
import play.api.Configuration 
import play.api.Mode 


class AccountDAOSpec extends Specification with MockitoSugar { // we add mockito to show that you can also bind your mocks 


val companyAccountDAOMock = mock[CompanyAccountDAO] // let us create a company account DAO mock 

    def app = new GuiceApplicationBuilder() // you create your app 
    .configure(
     Configuration.from(
     Map(// a custom configuration for your tests only 
      "slick.dbs.default.driver" -> "slick.driver.H2Driver$", 
      "slick.dbs.default.db.driver" -> "org.h2.Driver", 
      "slick.dbs.default.db.connectionPool" -> "disabled", 
      "slick.dbs.default.db.keepAliveConnection" -> "true", 
      "slick.dbs.default.db.url" -> "jdbc:h2:mem:test", 
      "slick.dbs.default.db.user" -> "sa", 
      "slick.dbs.default.db.password" -> ""))) 
    .bindings(bind[AccountDAO].to[AccountDAOImpl]) // here you can define your bindings for an actual implementation (note the use of square brackets) 
    .bindings(bind[CompanyAccountDAO].to(companyAccountDAOMock)) // or bind to your mock (note the use of parentheses) 
    .in(Mode.Test) 
    .build() 


    "AccountDAO" should { 

    "throw an Exception when adding a user with an invalid data" in new WithApplication(app) { // here you can use the application you just created, it uses the db you defined for your tests 

     val app2dao = Application.instanceCache[AccountDAO] 
     val accountDAO = app2dao(app) // with this you get the DAO injected 

     accountDAO.addAccount(testAccount).run must throwAn[Exception] 
    } 
    } 
} 
0

您應該刪除 「測試」 。 因此,你的第一行應該是:

running(fakeApplication(inMemoryDatabase()), new Runnable() { 
    //test specific code 
}); 
相關問題