简体   繁体   中英

Groovy / Mocking Sql

I'm trying to mock the Sql instance in Groovy the following way, I'm using the spock framework for testing. 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.

You try to verify it as an instance of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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