需要一些模拟sql.eachRow()的帮助。
我有一个要测试的服务,如下所示:
class MyService {
def dataSource
def method1(id) {
def map = [:]
def sql = new Sql(dataSource)
def queryString = 'select col1, col2 from tbl_1 where id = ?
sql.eachRow(queryString, [1]) { row ->
map.put(row.val1, row.val2)
}
return map
}
}
我正在尝试使用MockFor进行测试
代码:
class MyServiceTest extends Specification {
@Test
def "test method1"() {
setup:
def row = [:]
row["col1"] = "1"
row["col2"] = "val1"
def mockResult = [row]
Sql.metaClass.constructor = {dataSource -> return new MockSql("") }
def mockSql = new MockFor(Sql.class)
mockSql.demand.newInstance { def datasource ->
return mockSql
}
mockSql.demand.eachRow { def arg1, def arg2, closure ->
// run the closure over the mock array
mockResult.each(closure)
}
when:
def result = service.method1(1)
then:
result == ["1":"val1"]
}
}
使用earRow参数类型获取以下错误
No signature of method: com.kenexa.assess.MockSql.eachRow() is applicable for argument types: (java.lang.String, java.util.ArrayList, xyz_closure103) values: [select col1, col2 from tbl_1 where id = ?]
Possible solutions: eachRow(java.lang.Object, groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: com.kenexa.assess.MockSql.eachRow() is applicable for argument types: (java.lang.String, java.util.ArrayList, xyz_closure103) values: [select col1, col2 from tbl_1 where id = ?]
Possible solutions: eachRow(java.lang.Object, groovy.lang.Closure)