【问题标题】:Groovy / Mocking SqlGroovy / 模拟 Sql
【发布时间】: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 不是StringUrl 可能吗?我猜这是不匹配的部分(缺少一个撇号)。
  • 如果您觉得我的回答有用,请接受并点赞。

标签: unit-testing testing groovy mocking spock


【解决方案1】:

注意唯一不匹配的参数是db link。

您尝试将其验证为String 的实例:

'jdbc:mysql://localhost:666/db'

但在无与伦比的调用中它是:

jdbc:mysql://localhost:666/db

所以问题来了,它到底是什么?验证类型,您将解决问题。

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多