0

我怎样才能获得运行功能的方法?我想用不同的数据集准备数据库。此数据集应使用如下注释定义:

@PrepareDB("dataset1")
def "feature 1"() {

}

并且应该在这样的设置方法中使用:

def setup() {
    def dataset = currentTestMethod.getAnnotation().value //pseudo method
    prepareDB(dataset)
}

我对 JUnit4 做了同样的事情。我使用@Rule 获取当前方法名称并获取每个反射的注释值。我怎么能在 spock 中做到这一点?

更新:我自己找到了解决方案。使用 JUnit4 中的 TestWatcher 可以从当前运行的测试方法中获取注释:

@Rule
public TestRule watcher = new TestWatcher() {
    protected void starting(Description description) {
        println description.getAnnotation(PrepareDB.class).value()
    };
};
4

1 回答 1

1

I recommend to implement the database logic as an annotation-driven Spock extension or a JUnit rule, both of which provide easy access to the annotations of the executed feature method.

To answer your question, to get at the annotation from the setup method, you'd use (as of Spock 0.7) specificationContext.iterationInfo.parent.featureMethod.reflection.getAnnotation(PrepareDB). In Spock 1.0-SNAPSHOT and beyond, this has changed to specificationContext.currentFeature.featureMethod.getAnnotation(PrepareDB).

于 2013-04-08T06:22:51.323 回答