I have several tests as part of a WordSpec. From my reading of Scalatest docs, this should create a suite of tests. HSQLDB is being restarted for each of the test cases in this file.
@RunWith(classOf[JUnitRunner])
class UserAgentTest extends WordSpec with BeforeAndAfterAll {
val userService: UserService = new UserServiceJpaImpl
var userAgent: ActorRef = _
var user: MutableUser = _
override def beforeAll(configMap: Map[String, Any]) {
TestUtil.deleteAllTestUsers()
user = TestUtil.createTestUser("joe")
user.cash = 500
user.exp = 10000
user.level = 10
userService.save(user)
userAgent = actorOf(new UserAgent(user.id)).start()
}
override def afterAll(configMap: Map[String, Any]) {
if (userAgent != null)
userAgent.stop()
}
"UserAgent" must {
"test 1..." in { ... }
"test 2..." in { ... }
}
}
The result is that the test data loaded in beforeAll is not present for test 2. I can make this work by initializing the DB for each test (using "before" instead of "beforeAll"). This is not a problem for a small test like this, but may be an issue later. I am running this with Maven (it has issues when run from inside my IDE)
I've also noticed that when I run "mvn test", it appears to create a single HSQLDB instance for all my test (meaning, that my above example will succeed). When I run the above test as "mvn test -Dtest=UserAgentTest", it will fail (it appears to create multiple HSQLDB instances).
Unfortunately, running with "mvn test" will cause some other tests of mine to fail, as the single HSQLDB instance is used for all of my test suites.
My question, is how can I get my test setup to create one (and only one) instance of HSQLDB for each test suite.