如何在用scala编写的JUnit 4测试中实现@BeforeClass语义?

时间:2011-04-22 02:19:41

标签: scala annotations junit4

我继承了一些用scala编写的JUnit测试,需要修复它们才能使用@BeforeClass语义。我知道@BeforeClass注释只能应用于静态方法。我知道“伴侣”对象(与scala类相对)中定义的方法是静态的。如何在测试类中的单个实例方法之前调用一次测试方法?

3 个答案:

答案 0 :(得分:17)

object TestClass {

  @BeforeClass
  def stuff() {
    // beforeclass stuff
  }

}

class TestClass {

  @Test
  ...

}

似乎有用......

答案 1 :(得分:2)

我不得不转向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"
  }
}

答案 2 :(得分:0)

你没有指明你是否意味着在OO编程意义上继承或者从别人那里“接管”这个词。

在后一种情况下,我建议你使用ScalaTest或Specs2重新编写它,并将其作为JUnit测试公开(两个框架都支持这个),以便它可以与任何其他工具和进程集成已经到位。