Home How I create a Sql mock unit test Grails
Reply: 2

How I create a Sql mock unit test Grails

Pedro Henrique
1#
Pedro Henrique Published in 2018-02-02 19:21:48Z

I'm Trying to unit test Grails and what I have tried is given below:

def getMarca(CrDocumento crDocumento) {
    if (!crDocumento) {
        return null
    }
    String sql = ""

    sql = "select marca.id as marca_id from cr_documento, matricula, cr_renegoc_boleto, oferta_polo_turma, oferta_polo, oferta, marca where cr_documento.id = cr_renegoc_boleto.cr_documento_id and cr_documento.matricula_id = matricula.id and matricula.oferta_polo_turma_id = oferta_polo_turma.id and oferta_polo_turma.oferta_polo_id = oferta_polo.id and oferta_polo.oferta_id = oferta.id and oferta.marca_id = marca.id and cr_documento.id = $crDocumento.id"

    def sqlGroovy = new Sql(dataSource)
    def marcaId = sqlGroovy.firstRow(sql)?.marca_id
    return Marca.findById(marcaId)
}

but I can't mock Sql Class and see this error:

groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method groovy.sql.Sql#.

Cannot resolve which method to invoke for [null] due to overlapping prototypes between: [interface java.sql.Connection]
[interface javax.sql.DataSource]

James Kleeh
2#
James Kleeh Reply to 2018-02-02 21:49:01Z

The code that queries the id should be in a separate service that you can then mock.

Chip
3#
Chip Reply to 2018-02-02 22:13:37Z

From this Forum:

Service/datasource definition has to happen outside of the method. Spring will then inject the beans.

Try something like this:

FooController { 
        def dataSource_mydb 

        def barAction() { 
                def sql = new Sql(dataSource_mydb) 
        } 
} 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321823 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO