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

发布于 2024-11-03 05:51:07 字数 151 浏览 3 评论 0原文

我继承了一些用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-残月青衣踏尘吟 2024-11-10 05:51:07
object TestClass {

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

}

class TestClass {

  @Test
  ...

}

似乎有效...

object TestClass {

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

}

class TestClass {

  @Test
  ...

}

seems to work...

非要怀念 2024-11-10 05:51:07

我必须转向 specs2 才能使用 Scala 实现此功能。只是添加一个示例来帮助那些与原始海报有相同问题但还不知道规格2的人。

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"
  }
}

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:

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"}
}

And here's an example with a non-mutable specification:

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"
  }
}
瑕疵 2024-11-10 05:51:07

您没有指定您的意思是面向对象编程意义上的继承还是该词的“从其他人那里接管”的意义上。

在后一种情况下,我建议您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文