在内存db play框架中运行测试

mal*_*ing 5 testing automated-tests scala playframework specs2

我正在尝试为我的应用程序运行一些测试.应该可以在一个全新的内存数据库中运行测试,但我不会让它工作.

我的测试现在看起来像这样:

"Server" should {

"persist data for personal user properly" in {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {

    //Create personal users
    val add1 = route(FakeRequest(POST, "/rest/personaluser").withFormUrlEncodedBody("name" -> "user1" , "email" -> "email@test1.com", "password" -> "test123", "gender" -> "male", "birthdate" -> "Oct 1, 2013", "nationality" -> "Sweden")).get
    val add2 = route(FakeRequest(POST, "/rest/personaluser").withFormUrlEncodedBody("name" -> "user2" , "email" -> "email@test2.com", "password" -> "test123", "gender" -> "male", "birthdate" -> "Oct 1, 2013","nationality" -> "Sweden")).get
    status(add1) must equalTo(OK)
    status(add2) must equalTo(OK)

    //Count users
    personalUserRepository.getAllPersonalUsers().length must beEqualTo(2)

    //Verify users exist
    personalUserRepository.checkIfPersonalUserExists("email@test1com") must beTrue
    personalUserRepository.checkIfPersonalUserExists("email@test2com") must beTrue

    //Verify user don't exist
    personalUserRepository.checkIfPersonalUserExists("email@test3com") must beFalse

    //Find user by email
    val findUser = route(FakeRequest(GET, "/rest/personaluserbyemail/email@test1.com")).get
    status(findUser) must equalTo(OK)
    contentAsString(findUser) must /("name" -> "user1")
    contentAsString(findUser) must /("email" -> "email@test1.com")
    contentAsString(findUser) must /("gender" -> "male")
    contentAsString(findUser) must /("nationality" -> "Sweden")
    contentAsString(findUser) must /("facebookID" -> "0")

  }
 }
}
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我得到了错误InconsistentDatabase: Database 'default' is in inconsistent state!.这是因为标准inMemoryDB可能不支持我用于默认数据库的MySQL吗?

但是,我尝试添加像这样的memoryDB:

在这里定义它

def memoryDB = Map("db.default.url" -> "jdbc:h2:mem:playdb;MODE=MYSQL;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;TRACE_LEVEL_SYSTEM_OUT=1")
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

"Server" should {

"persist data for personal user properly" in {
  running(FakeApplication(additionalConfiguration = memoryDB)) {
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,它不使用内存db,测试失败,//Count users因为它不等于2,但是7.它仍然使用真正的数据库,而不是我尝试使用的新的新内存db在这个FakeApplication中.

我做错了什么或我错过了什么?

任何能让我走上正轨的答案都非常感谢!谢谢!

joe*_*cii 0

首先我要说的是我对 Play 不太熟悉。您从未提及如何配置 JDBC 驱动程序。我怀疑在测试模式下,应用程序仍在对 H2 DB 使用 MySQL 驱动程序类。您的测试类路径中还需要 H2 jar 文件。