【发布时间】:2020-12-21 13:05:36
【问题描述】:
我的目标是Mock Grails 中服务类方法中的私有变量。
在这里,我在测试方法中尝试了以下方式:
given: 'Mocking of object'
def dataSource = Mock(TransactionAwareDataSourceProxy)
def db1 = Mock(Sql)
service.dataSource = dataSource
new Sql(dataSource) >> db1
List<GroovyRowResult> resultList = new ArrayList<>()
GroovyRowResult result = new GroovyRowResult(id: 0)
result.someAmount = 400
resultList.add(result)
db1.rows(_) >> resultList
在我的服务类中,我的代码是:
def db = new Sql(dataSource)
List<GroovyRowResult> resultList = db.rows("Select * from user_info")
在这里,我成功地模拟了名为 dataSource 的 TransactionAwareDataSourceProxy,但我未能将模拟 def db = new Sql(dataSource) 分配给本地私有变量 db。
我需要以下解决方案:
- 如何在方法中模拟私有变量。在这里,我在我的服务方法中的私有变量
db中分配Sql
提前致谢
【问题讨论】:
标签: sql unit-testing grails spock