0
votes

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.

1
Please edit your text and make it clear exactly what your questions are.fredt

1 Answers

0
votes

With a single instance of HSQLDB for all tests, if some test suites need to start with an empty database, you can issue this statement to clean up the old data at the beginning or end of each suite, depending on settings:

DROP SCHEMA PUBLIC CASCADE

This can also be used for any schema created by your tests.

Other options include connecting to a server containing a memory database, which allows the database to survive multiple executions in different processes.