【发布时间】:2015-03-15 18:12:33
【问题描述】:
我正在尝试通过以下方式在 Groovy 中模拟 Sql 实例,我正在使用 spock 框架进行测试。但是测试失败了,请看下面:
class SQLStatsStorageManagerTest extends Specification {
def mockSql
def setup() {
mockSql = GroovyMock(Sql, global: true)
}
void "SQLStatsStorageManager instantiation succeed"() {
def c
when: "SQLStatsStorageManager is instantiated"
c = new SQLStatsStorageManager("test", [hostname: "localhost", port: 666, database: "db", login: "root", password: "pass"])
then: "there is no error and name is set"
1 * mockSql.newInstance('jdbc:mysql://localhost:666/db', 'root', 'pass', 'com.mysql.jdbc.Driver')
assert c.getName() == "test"
}
}
测试失败并出现以下错误:
Too few invocations for:
1 * mockSql.newInstance('jdbc:mysql://localhost:666/db', 'root', 'pass', 'com.mysql.jdbc.Driver') (0 invocations)
Unmatched invocations (ordered by similarity):
1 * mockSql.newInstance(jdbc:mysql://localhost:666/db, 'root', 'pass', 'com.mysql.jdbc.Driver')
有什么想法吗?
谢谢。
【问题讨论】:
-
似乎在无与伦比的调用中
jdbc:mysql://localhost:666/db不是String,Url可能吗?我猜这是不匹配的部分(缺少一个撇号)。 -
如果您觉得我的回答有用,请接受并点赞。
标签: unit-testing testing groovy mocking spock