A new scala-play project (v2.5.6) starts with tests examples like this one:
"HomeController" should {
"render the index page" in {
val home = route(app, FakeRequest(GET, "/")).get
status(home) mustBe OK
}
}
and controller examples like this one, injecting a Database with Guice:
class DataManagementController @Inject()(db: Database) extends Controller {...}
I would like to test this new controller as above, but injecting a test in-memory database instead of the "default". After hours trying I can't manage to have that. I found clues in
(and external posts using a FakeApplication, although they are very complicated and I'd prefer to follow the official, current version's docs.)
From that I understood that there is an implicit app argument used in route(app, ...), so I tried to override it:
Attempt 1: (compiles but still uses database "default")
class MySpec extends PlaySpec with OneAppPerSuite {
implicit override lazy val app = new GuiceApplicationBuilder()
.configure(inMemoryDatabase("test").build()
"DataManagementController" should { ... }
Attempt 2: (does not compile: "Cannot resolve symbol 'TestDb'")
class MySpec extends PlaySpec with OneAppPerSuite {
val TestDb = Databases.inMemory(name="test")
implicit override lazy val app = new GuiceApplicationBuilder()
.overrides(bind[Database].to[TestDb]).build()
"DataManagementController" should { ... }
Am I on the right track?
Shouldn't be an issue, but you can try:
implicit override lazy val app = new GuiceApplicationBuilder()
.overrides(bind(classOf[Database]).to(classOf[TestDb]).build()
instead of
implicit override lazy val app = new GuiceApplicationBuilder()
.overrides(bind[Database].to[TestDb]).build()
and/or
"" should {
"" in new App(appBuilder) {
}
}
where appappBuilder is GuiceApplicationBuilder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With