Recently I was trying to do some cleanup of shared DB after the tests (with ScalaTest) of Play application using afterAll method of BeforeAndAfterAll trait. DB credentials were contained in Play’s config files so cleanup code relied on running play application. But when trying to run the test I’ve got an exception:

java.lang.RuntimeException: There is no started application

Looks like application was shut down before afterAll gets started. Turns out that traits inclusion order matters here. Following code throws an exception:

class MySpec extends PlaySpec 
with OneServerPerSuite
with BeforeAndAfterAll {
//tests go here
override def afterAll(): Unit = {
//cleanup code goes here

while this works perfectly:

class MySpec extends PlaySpec
with BeforeAndAfterAll
with OneServerPerSuite ...

Same applies to OneAppPerSuite. The reason is order of overriding method. BeforeAndAfterAll attaches there handling of beforeAll and afterAll methods, and OneServerPerSuite attaches testServer.start() and testServer.stop(). Didn’t check it, but appears that due to this ordering only one of beforeAll and afterAll can have access to Play application. So, now beforeAll will fail to access application configs.