Grails Spock错误groovy.lang.MissingMethodException:当一个方法调用另一个方法时
当运行单元测试时,当被测试的方法调用被测试对象内部的另一个方法时,我得到一个关于spoc闭包失败的错误 错误是:Grails Spock错误groovy.lang.MissingMethodException:当一个方法调用另一个方法时,grails,testing,groovy,spock,Grails,Testing,Groovy,Spock,当运行单元测试时,当被测试的方法调用被测试对象内部的另一个方法时,我得到一个关于spoc闭包失败的错误 错误是: groovy.lang.MissingMethodException: No signature of method: com.mypackage.RuleRunnerServiceSpec$__spock_feature_0_9_closure12.doCall() is applicable for argument types: ([B, java.lang.Integer,
groovy.lang.MissingMethodException: No signature of method: com.mypackage.RuleRunnerServiceSpec$__spock_feature_0_9_closure12.doCall() is applicable for argument types: ([B, java.lang.Integer, java.lang.Integer) values: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...], ...]
Possible solutions: call(), doCall(java.io.InputStream), findAll()
at com.mypackage.RuleRunnerService.loadRulesFromS3(RuleRunnerService.groovy:132)
at com.mypackage.RuleRunnerServiceSpec.should load from s3(RuleRunnerServiceSpec.groovy:272)
测试是:
void "should load from s3"(){
given:
RuleRunnerService ruleService = RuleRunnerService.instance
ruleService.grailsApplication = [
config: [
alert: [
engine: [s3 : [bucketName: 'bucket']]
]
]
]
def s3wrapper = mockFor(S3Wrapper, true)
s3wrapper.demand.asBoolean(0..999) { -> true }
s3wrapper.demand.getS3ObjectToInputStream(0..999){
InputStream stream -> new FileInputStream('test/resources/samples-drl/samplefile.drl')
}
ruleService.s3 = s3wrapper.createMock()
when:
ruleService.loadRulesFromS3('test')
then:
ruleService.hasRulePackageByName('test')
正在测试的方法是:
void loadRulesFromS3(String organizationId){
String bucketName = grailsApplication.config.alert.engine.s3.bucketName
S3Wrapper s3 = getS3Wrapper()
InputStream newRules = s3.getS3ObjectToInputStream(bucketName, organizationId)
loadRulesFromString(organizationId, [newRules.text])
}
调用退出时,loadRulesFromString(…)调用出现上述错误。在Spock测试中是否需要一些设置来允许内部方法调用 我们看不到测试的其他内容,但可能是这样的:
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(RuleRunnerService)
class RuleRunnerServiceSpec extends Specification {
def "shouldLoadFromS3"() {
given:
grailsApplication.config.alert.engine.s3.bucketName = 'bucket'
def s3WrapperMock = mockFor(S3Wrapper, true)
s3WrapperMock.demand.with {
asBoolean(0..99) { -> true }
getS3ObjectToInputStream(0..999) { InputStream stream ->
new FileInputStream('test/resources/samples-drl/samplefile.drl')
}
}
service.s3 = s3WrapperMock.createMock()
when:
service.loadRulesFromS3('test')
then:
service.hasRulePackageByName('test')
}
}
或者类似的东西(除了打字错误)。我在Spock规范中一直使用Groovy Mock
理想情况下,loadRulesFromS3的单元测试将该方法作为测试中唯一的“变量”。使用hasRulePackageByName(我假设它是服务中的另一个方法)来确定loadRulesFromS3的有效性,从技术上讲,这将为测试引入另一个变量。如果hasRulePackageByName方法中存在错误,则loadRulesFromS3的此测试可能会失败。这种风险是否具有后果取决于您。您应该发布您的“s3wrapper”,因为您没有使用斯波克的模拟