如何在用 scala 编写的 JUnit 4 测试中实现 @BeforeClass 语义?
我继承了一些用 scala 编写的 JUnit 测试,需要修复这些测试才能使用 @BeforeClass 语义。我知道 @BeforeClass 注释必须仅应用于静态方法。我知道“同伴”对象(而不是 scala 类)中定义的方法是静态的。如何在测试类中的各个实例方法之前调用一次测试方法?
I've inherited some JUnit tests written in scala that need to be fixed to use @BeforeClass semantics. I understand that the @BeforeClass annotation must be applied to static methods only. I understand that methods defined in "companion" objects (as opposed to scala classes) are static. How can I get a test method to be called once prior to the individual instance methods in a test class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎有效...
seems to work...
我必须转向 specs2 才能使用 Scala 实现此功能。只是添加一个示例来帮助那些与原始海报有相同问题但还不知道规格2的人。
Specs2 方式使用“步骤”的概念来完成测试套件的设置和拆卸。如果您使用 JUnitRunner 运行,所有使用 JUnit 的 Ant 脚本和 IDE 仍然知道如何运行它。这是一个使用 specs2 中的可变规范的示例:
这是一个使用非可变规范的示例:
I had to move to specs2 to implement this feature with Scala. Just adding an example to help people with the same problem as the orginal poster who don't yet know specs2.
The specs2 way uses the concept of a "step" to accomplish test suite set-up and tear-down. If you run with a JUnitRunner all your Ant scripts and IDEs that use JUnit will still know how to run it. Here's an example using a mutable specification from specs2:
And here's an example with a non-mutable specification:
您没有指定您的意思是面向对象编程意义上的继承还是该词的“从其他人那里接管”的意义上。
在后一种情况下,我建议您使用 ScalaTest 或 Specs2 重新编写该内容,并将其公开为 JUnit 测试(两个框架都支持这一点),以便它将与您已经拥有的任何其他工具和流程集成。地方。
You don't specify if you mean inherited in the OO-programming sense or the "taken over from someone else" sense of the word.
In the latter case, I'd advise you to re-write the thing using either ScalaTest or Specs2 and expose it as a JUnit test (both frameworks support this) so that it'll integrate with whatever other tools and processes you already have in place.