Groovy获取当前方法注释
我为注释创建了一个java接口。我现在正在编写一个geb spock测试,我想打印注释值,以便它显示在gradle报告中。这可能吗?这是我的测试用例,如果我做错了,请告诉我Groovy获取当前方法注释,groovy,geb,Groovy,Geb,我为注释创建了一个java接口。我现在正在编写一个geb spock测试,我想打印注释值,以便它显示在gradle报告中。这可能吗?这是我的测试用例,如果我做错了,请告诉我 class Checkout extends GebReportingSpec { @TestCase(someID="12345") def "A checkout 3-D script"() { // My test steps..... } } 用于获取当前方法,并使用反射迭
class Checkout extends GebReportingSpec {
@TestCase(someID="12345")
def "A checkout 3-D script"() {
// My test steps.....
}
}
用于获取当前方法,并使用反射迭代注释:
import java.lang.annotation.*
import org.codehaus.groovy.runtime.StackTraceUtils
class Checkout {
@TestCase(someID="12345")
def "yeah a"() {
printTestCaseId()
// My test steps.....
}
def printTestCaseId() {
def stack = StackTraceUtils.sanitize(new Throwable()).stackTrace[1]
def method = getClass().declaredMethods.find { it.name == stack.methodName }
println method
def someID = method.annotations[0].someID()
println someID
assert someID == "12345"
}
}
@Retention (RetentionPolicy.RUNTIME)
@interface TestCase { String someID() }
co = new Checkout()
co."${'yeah a'}"()
如果您是迭代这些方法的人,则不需要使用StackTraceUtils。spockframework(版本“spock-core-1.1-groovy-2.4”)提供了访问注释的方法:
package com.test.integration.spec
import com.test.integration.annotation.Scenario
import com.test.integration.annotation.TestCase
import spock.lang.Specification
@Scenario("AnnotationSpec")
class AnnotationSpec extends Specification {
String scenario
String test_case
def setup() {
scenario = specificationContext.currentSpec.getAnnotation(Scenario).value()
test_case = specificationContext.currentFeature.featureMethod.getAnnotation(TestCase).value()
}
@TestCase("case-001")
def 'spock provides way of accessing annotation'(){
expect:
"AnnotationSpec" == scenario
"case-001" == test_case
}
}
@Retention stuff是我注释代码的替代品吗?不是。我写它是为了复制你的
@TestCase
注释OK,当我把你的代码放在@Retention行之外时,我得到了groovy.lang.MissingMethodException:没有方法签名:错误