Prasann
Prasann

Reputation: 1291

Groovy unit test case

I am very new to groovy. I am trying to write testcase for my groovy class, but unable to do so as I am not sure where to start it from.

I have a groovy class as below. Can anyone please let me know how should the test class look like? And if there are any good tutorial for the same?

Tried to find information via google, but couldn't find much help. Thanks

class AddressController {

    def index = { redirect(action:list,params:params) }

    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ addressInstanceList: Address.list( params ), addressInstanceTotal: Address.count() ]
    }

    def show = {
        def addressInstance = Address.get( params.id )

        if(!addressInstance) {
            flash.message = "Address not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ addressInstance : addressInstance ] }
    }

    def update = {
        def addressInstance = Address.get( params.id )
        if(addressInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(addressInstance.version > version) {

                    addressInstance.errors.rejectValue("version", "address.optimistic.locking.failure", "Another user has updated this Address while you were editing.")
                    render(view:'edit',model:[addressInstance:addressInstance])
                    return
                }
            }
            addressInstance.properties = params
            if(!addressInstance.hasErrors() && addressInstance.save()) {
                flash.message = "Address ${params.id} updated"
                redirect(action:show,id:addressInstance.id)
            }
            else {
                render(view:'edit',model:[addressInstance:addressInstance])
            }
        }
        else {
            flash.message = "Address not found with id ${params.id}"
            redirect(action:list)
        }
    }
}

Upvotes: 0

Views: 665

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42184

The code you shown us is a controller class from Grails framework. I assume this is 2.x version (or even older). You can check it out in application.properties file under app.grails.version property.

When you get know what is the actual version of Grails framework I would recommend starting from reading official documentation on unit testing (http://docs.grails.org/2.5.6/guide/testing.html#unitTesting - here is the chapter describing unit testing for version 2.5.6).

Your controller represents regular CRUD (acronym of Create-Read-Update-Delete) operations. There are multiple cases you could test like:

  • listing all addresses when no address exist
  • listing all addresses when at least one address exists
  • reading single address
  • creating address
  • updating non-existing address
  • updating existing address
  • deleting address etc.

Read carefully chapter "Unit Testing Controllers" where you can find several examples of testing controllers in Grails. Keep in mind that if you decide to unit test controller that uses GORM domain objects you will have to mock every domain object with @Mock([YourDomainClassName]) annotation added over test case class (it's described in details here: http://docs.grails.org/2.5.6/guide/testing.html#unitTestingDomains)

Upvotes: 1

Related Questions