无法调用groovy闭包

时间:2014-09-15 21:48:02

标签: soap groovy

我正在使用ws-lite来自动化Web服务测试,我希望能够更灵活地控制生成的xm l请求。

基本上,请求体是一个groovy闭包,它将被传递给MarkupBuilder以创建SOAP消息。

这是我想要实现的一个例子(例子来自https://github.com/jwagenleitner/groovy-wslite):

@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*

def client = new SOAPClient('http://www.holidaywebservice.com/Holidays/US/Dates/USHolidayDates.asmx')
def month = ["Feb"]

def response = client.send(SOAPAction:'http://www.27seconds.com/Holidays/US/Dates/GetMothersDay') {
    body {
        GetMothersDay('xmlns':'http://www.27seconds.com/Holidays/US/Dates/') {
            year(2011)
            month.each{ a = null ->
                                        if (a != null){
                                            "month"(a)
                                        }
                                    }
        }
    }
}

assert "2011-05-08T00:00:00" == response.GetMothersDayResponse.GetMothersDayResult.text()
assert 200 == response.httpResponse.statusCode
assert "ASP.NET" == response.httpResponse.headers['X-Powered-By']

上面的示例,我可以使用指定的值/值创建月份标记。

但如果我将其改为:

@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*

def client = new SOAPClient('http://www.holidaywebservice.com/Holidays/US/Dates/USHolidayDates.asmx')
def month_cl = { a -> "month"(a) }

def response = client.send(SOAPAction:'http://www.27seconds.com/Holidays/US/Dates/GetMothersDay') {
    body {
        GetMothersDay('xmlns':'http://www.27seconds.com/Holidays/US/Dates/') {
            year(2011)
            month_cl("Feb")
        }
    }
}

assert "2011-05-08T00:00:00" == response.GetMothersDayResponse.GetMothersDayResult.text()
assert 200 == response.httpResponse.statusCode
assert "ASP.NET" == response.httpResponse.headers['X-Powered-By']

我将缺少方法异常。

我不太明白为什么我不能像这样调用一个时髦的封口?

2 个答案:

答案 0 :(得分:1)

month_cl闭包的委托必须设置为当前/父委托(在这种情况下,它是作为param传递给GetMothersDay的闭包)。试试:

body {
    GetMothersDay('xmlns':'http://www.27seconds.com/Holidays/US/Dates/') {
        year(2011)
        month_cl.delegate = delegate
        month_cl("Feb")
    }
}

答案 1 :(得分:0)

获取MissingMethodException非常正常,因为闭包month_cl正在调用不存在的month方法。为此(以您的方式),您应该将Closure c传递给month_cl并在参数a上调用它,如下所示:

def month_cl = { a, Closure c -> c(a) }

并使用新的实施,month_cl("Feb")变为month_cl("Feb") { "month" },结果为"month"("Feb")

这是一个有效的例子:

@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*

def client = new SOAPClient('http://www.holidaywebservice.com/Holidays/US/Dates/USHolidayDates.asmx')
def months = ["Feb"]
def month_cl = { m, Closure c -> return c(m) }

def response = client.send(SOAPAction:'http://www.27seconds.com/Holidays/US/Dates/GetMothersDay') {
    body {
        GetMothersDay('xmlns':'http://www.27seconds.com/Holidays/US/Dates/') {
            year(2011)
            month_cl("Feb") { "month" }  // -> month("Feb")
        }
    }
}

assert "2011-05-08T00:00:00" != response.GetMothersDayResponse.GetMothersDayResult.text()
assert 200 == response.httpResponse.statusCode
assert "ASP.NET" == response.httpResponse.headers['X-Powered-By']
相关问题