简体   繁体   中英

How i can get the method in the setup method in spock?

how i can get the method of the running feature? I want to prepare the database with different datasets. This datasets should be defined with annotations like this:

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

}

and should be used in the setup method like this:

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

I did the same with JUnit4. I used @Rule to get current method name and get the annotation value per reflection. How i can do this in spock?

Update: I found a solution myself. With the TestWatcher from JUnit4 its possible to get an annotation fom the current running test method:

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

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM