单元测试中的 HSQLDB (Scalatest / JUnitRunner)
我有几个测试作为 WordSpec 的一部分。根据我对 Scalatest 文档的阅读,这应该创建一套测试。正在为此文件中的每个测试用例重新启动 HSQLDB。
@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 { ... }
}
结果
是测试 2 中不存在 beforeAll 中加载的测试数据。我可以通过为每个测试初始化数据库(使用“before”而不是“beforeAll”)来完成这项工作。对于这样的小测试来说这不是问题,但以后可能会出现问题。我正在使用 Maven 运行它(从 IDE 内部运行时会出现问题)
我还注意到,当我运行“mvn test”时,它似乎为我的所有测试创建了一个 HSQLDB 实例(也就是说,我上面的示例将会成功)。当我以“mvn test -Dtest=UserAgentTest”运行上述测试时,它将失败(它似乎创建了多个 HSQLDB 实例)。
不幸的是,使用“mvn test”运行将导致我的一些其他测试失败,因为单个 HSQLDB 实例用于我的所有测试套件。
我的问题是,如何让我的测试设置为每个测试套件创建一个(且仅有一个)HSQLDB 实例。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于所有测试使用单个 HSQLDB 实例,如果某些测试套件需要从空数据库开始,您可以发出此语句来清理每个套件开头或结尾的旧数据,具体取决于设置:
这也可以用于您的测试创建的任何模式。
其他选项包括连接到包含内存数据库的服务器,这允许数据库在不同进程中的多次执行中幸存下来。
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:
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.