【发布时间】:2011-04-22 02:19:40
【问题描述】:
我继承了一些用 scala 编写的 JUnit 测试,这些测试需要修复才能使用 @BeforeClass 语义。我知道 @BeforeClass 注释只能应用于静态方法。我知道在“伴侣”对象(而不是 scala 类)中定义的方法是静态的。如何在测试类中的各个实例方法之前调用一次测试方法?
【问题讨论】:
标签: scala annotations junit4
我继承了一些用 scala 编写的 JUnit 测试,这些测试需要修复才能使用 @BeforeClass 语义。我知道 @BeforeClass 注释只能应用于静态方法。我知道在“伴侣”对象(而不是 scala 类)中定义的方法是静态的。如何在测试类中的各个实例方法之前调用一次测试方法?
【问题讨论】:
标签: scala annotations junit4
object TestClass {
@BeforeClass
def stuff() {
// beforeclass stuff
}
}
class TestClass {
@Test
...
}
似乎有效...
【讨论】:
我不得不转移到 specs2 以使用 Scala 实现此功能。只是添加一个例子来帮助那些与原始海报有同样问题但还不知道 specs2 的人。
specs2 方式使用“步骤”的概念来完成测试套件的设置和拆卸。如果您使用 JUnitRunner 运行,所有使用 JUnit 的 Ant 脚本和 IDE 仍将知道如何运行它。下面是一个使用来自 specs2 的可变规范的示例:
import org.specs2.mutable.Specification
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
@RunWith(classOf[JUnitRunner])
class MutableSpecs2ExampleTest extends Specification {
var firstStep: String = null
var secondStep: String = null
var thirdStep: String = null
//Steps are guaranteed to run serially
step{println("Loading Spring application context...");firstStep="Hello World"}
step{println("Setting up mocks...");secondStep = "Hello Scala"}
//The fragments should be run in parallel by specs2
"Some component Foo in my project" should{
" pass these tests" in {
println("Excersizing some code in Foo")
firstStep must startWith("Hello") and endWith("World")
}
" pass theses other tests" in {
println("Excersizing some other code in Foo")
firstStep must have size(11)
}
}
"Some component Bar in my project" should{
" give the correct answer" in {
println("Bar is thinking...")
secondStep must startWith("Hello") and endWith("Scala")
thirdStep must be equalTo null
}
}
step{println("Tearing down resources after tests...");thirdStep = "Hello Specs2"}
}
这是一个不可变规范的示例:
import org.specs2.Specification
import org.specs2.specification.Step
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
@RunWith(classOf[JUnitRunner])
class Specs2ExampleTest extends Specification{
var firstStep: String = null
var secondStep: String = null
var thirdStep: String = null
def is =
"This is a test with some set-up and tear-down examples" ^
p^
"Initialize" ^
Step(initializeDependencies())^
Step(createTestData())^
"Component Foo should" ^
"perform some calculation X " !testX^
"perform some calculation Y" !testY^
p^
"Tidy up" ^
Step(removeTestData())^
end
def testX = {
println("testing Foo.X")
firstStep must be equalTo("Hello World")
}
def testY = {
println("testing Foo.Y")
secondStep must be equalTo("Hello Scala")
thirdStep must be equalTo null
}
def initializeDependencies(){
println("Initializing Spring applicaiton context...")
firstStep = "Hello World"
}
def createTestData(){
println("Inserting test data into the db...")
secondStep = "Hello Scala"
}
def removeTestData(){
println("Removing test data from the db...")
println("Tearing down resources...")
thirdStep = "Hello Specs2"
}
}
【讨论】:
您没有具体说明您是指 OO 编程意义上的继承还是“从其他人那里接管”这个词的含义。
在后一种情况下,我建议您使用 ScalaTest 或 Specs2 重写该东西并将其公开为 JUnit 测试(两个框架都支持这一点),以便它与您的任何其他工具和流程集成已经到位了。
【讨论】: