简体   繁体   English

Groovy /模拟SQL

[英]Groovy / Mocking Sql

I'm trying to mock the Sql instance in Groovy the following way, I'm using the spock framework for testing. 我正在尝试通过以下方式在Groovy中模拟Sql实例,我正在使用spock框架进行测试。 However the test fails, please see below: 但是测试失败,请参见以下内容:

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"
    }
}

The test fails with the following error: 测试失败,并出现以下错误:

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')

Any idea ? 任何想法 ?

Thanks. 谢谢。

Pay attention to the fact that the only argument that does not match is db link. 请注意,唯一不匹配的参数是db link。

You try to verify it as an instance of String : 您尝试将其验证为String的实例:

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

but in unmatched invocation it is: 但在无与伦比的调用中,它是:

jdbc:mysql://localhost:666/db

so here's the question, what it actually is? 所以这是问题,实际上是什么? Verify the types and you'll have the problem solved. 验证类型,即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM