After migrating an application to Play 2.4 and introducing dependency injection into the controller of the application, I'm getting "Pool has been Shutdown" when running unit tests. The affected tests are something like this:
@RunWith(classOf[JUnitRunner])
class ApplicationSpec extends Specification {
"Application" should {
"doSomething" in running(TestUtil.app) {
val myId = IdGen.newId("someone")
...
}
}
}
Where the IdGen class looks something like:
object IdGen {
def newId(name: String): ClientCredentials = {
DB.withTransaction("myDb") { implicit conn =>
...
}
}
}
The test fails on the DB.withTransaction() call with
[error] Pool has been shutdown (HikariDataSource.java:89)
[error] com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:89)
[error] play.api.db.DefaultDatabase.getConnection(Databases.scala:143)
[error] play.api.db.DefaultDatabase.withConnection(Databases.scala:153)
[error] play.api.db.DefaultDatabase.withTransaction(Databases.scala:162)
[error] play.api.db.DB$.withTransaction(DB.scala:72)
[error] com.example.idGen$.newId...
I'm initializing TestUtil.app with
object TestUtil {
lazy val app = new GuiceApplicationBuilder()
.configure(defaultConfig ++ Helpers.inMemoryDatabase("myDB"))
.bindings(new TestModule) // Mock injections for test
.build
}
Clearly I'm missing something to get the database up and running for tests, but I'm unsure what.
Solved this.
Tried to replace lazy val with def, as answered in this question: Testing: FakeApplication ignoring additionalConfiguration
Which solved the problem.
Would love if someone could explain why?