grails - Spock错误groovy.lang.MissingMethodException:当一种方法调用另一种方法时

标签 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, 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模拟。

理想情况下,对于loadRulesFromS3的单元测试会将该方法作为测试中唯一起作用的“变量”。使用hasRulePackageByName(我认为是服务中的另一种方法)来确定loadRulesFromS3的有效性是在技术上向测试引入了另一个变量。如果hasRulePackageByName方法中存在错误,则loadRulesFromS3的此测试可能会失败。该风险是否必然取决于您。

关于grails - Spock错误groovy.lang.MissingMethodException:当一种方法调用另一种方法时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247733/

相关文章:

grails - Grails 2.0 中带有新 where 查询的参数

grails - 在 Grails 中翻译 HTML 选择元素

apache - 在现有站点上使用 Tomcat 部署 Grails 应用程序

ruby - 在单个 Ruby 文件上使用 guard-minitest

ruby - 动态语言——我应该选择哪一种?

mysql - Jenkins 脚本控制台/Scriptler 中的连接器/J MySQL 驱动程序

mysql - 从 Grails 访问 MySQL 服务器

testing - 记录哪个测试执行器正在运行特定测试?

visual-studio - Visual Studio 2010 Web 性能测试/负载测试/编码的 UI 测试。任何人真的使用这些?

java - 如何在katalon studio中将WebElement转换为TestObject?