1

我们正计划构建一个基于 spring 的系统,并且我们想使用一个基于 groovy 的 BDD 框架。该系统也是基于 OSGi 的。

有什么关于一个好的 BDD 框架的建议,它也适合 Spring 的 STS eclipse。?

在这样的环境中,您将如何使用模拟?OSGi 需要模拟很多外部依赖项。

谢谢!

4

1 回答 1

2

Spock 测试框架是一个基于 Groovy 的强大的受 BDD 启发的测试框架。它有很多功能,可能正是您想要的。

不要被低版本号(当前版本是 0.7)吓到,它已经稳定并准备好生产很长时间了。我已经使用它至少几年来测试 Java、Groovy、Griffon 和 Grails 项目——并且不会梦想回去......

它建立在 JUnit 之上,将在 JUnit 运行的每个环境中运行(至少我知道)。它作为普通的 JUnit 测试在 Eclipse 和 IntelliJ IDEA 中运行。

expect:一个非常简单的例子(注意:后面的注释where:等是可选的):

import spock.lang.Specification

class SpockExampleSpecification extends Specification {

    def "a String should return correct length"() {
        expect: "correct length"
        myString.length() == length

        where: "different strings have different lengths"
        myString    | length
        "hello"     | 5
        "abc"       | 3
        ""          | 0
    }

    def """show a string mock that could be injected into a class;
           using groovy metaClass, spring injection or any other means"""() {

        given: "a mock for char sequence, injected into DoubleLength"
        def mock = Mock(CharSequence)
        def dsl = new DoubleLength(myString: mock)

        when: "accessing lengths from this mock"
        def doubleLength = dsl.length()

        then: """mock value should be used and we should
                 get the expected interactions with the mock"""
        1 * mock.length() >> 1234
        doubleLength == 2468
    }

}

class DoubleLength {
    def myString

    def length() {
        2 * myString.length()
    }
}
于 2013-03-11T18:55:52.670 回答