2015-02-06 9 views
8

Sto provando a configurare la mia applicazione framework di gioco in modo che utilizzi un database mysql durante l'esecuzione e un database in memoria per i test. Quando eseguo i test si connette al database mysql e non al database in memoria. Qualcuno sa perché?play framework utilizza nel database di memoria h2 per i test di unità

Questo è il mio config:

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="" 

questa è la mia prova:

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

Un altro buon approccio sarebbe quello di utilizzare h2 per DEV e TEST in application.conf, una da utilizzare per MySQL in modalità di produzione solo con un file separato application-prod.conf – Loic

+0

Sì, penso che sarà la soluzione che andrà male. Speravo che esistesse un modo più semplice/pulito per farlo. Grazie – Ciaran0

+1

Come nota a margine: utilizzo MariaDB4j come database incorporato per i test. È un vero database compatibile MySQL invece della modalità MySQL incompleta di H2. https://github.com/vorburger/MariaDB4j – akkie

risposta

1

Da un'applicazione effettiva sto sviluppando:

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

si dovrebbe rimuovere "test" . Così la prima linea dovrebbe essere:

running(fakeApplication(inMemoryDatabase()), new Runnable() { 
    //test specific code 
}); 
Problemi correlati