Larx
Larx

Reputation: 161

Controller always null in Spock unit test using Grails 2.5.1

I am new using Grails 2.5.1. I need to run some unit and integration test but I can't make them work.

My domain class is:

class Role {

String roleName

Role(String _roleName) {
    roleName = _roleName
}

static constraints = {
    roleName(blank: false, nullable: false)
}
String toString(){
    "$roleName"
}

}

My controller class is:

class RoleController {

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond Role.list(params), model:[roleInstanceCount: Role.count()]
}

def show(Role roleInstance) {
    respond roleInstance
}

def create() {
    respond new Role(params)
}

...
}

Under test/unit I have the class RoleControllerSpec:

import grails.test.mixin.*
import spock.lang.*

@TestFor(RoleController)
@Mock(Role)
class RoleControllerSpec extends Specification {

def 'index action: 1 role'() {
    setup:
    roleInstance.save()

    expect:
    controller.index() == [roleInstanceList: [roleInstance], roleInstanceTotal: 1]

    where:
    roleInstance = new Role(roleName: "Role1")
}



def "create action"() {
    setup:
    controller.params.roleName = roleName

    when:

    def model = controller.create()

    then:
    model.roleInstance != null
    model.roleInstance.roleName == roleName


    where:
    roleName = "Role1"

}
}

When I run the test with test-app -unit RoleController it give me the following exceptions:

|Configuring classpath
.
|Environment set to test
....................................
|Running without daemon...
..........................................
|Compiling 1 source files
.
|Running 2 unit tests...

|Running 2 unit tests... 1 of 2
Failure:  |
index action: 1 role(accessmanagement.RoleControllerSpec)
 |
Condition not satisfied:
controller.index() == [roleInstanceList: [roleInstance], roleInstanceTotal: 1]
|          |       |                      |
|          null    false                  Role1
 role(RoleControllerSpec.groovy:17)

|Running 2 unit tests... 2 of 2
Failure:  |
create action(accessmanagement.RoleControllerSpec)
 |
java.lang.NullPointerException: Cannot get property 'roleInstance' on null object
at accessmanagement.RoleControllerSpec.create action(RoleControllerSpec.groovy:34)

|Completed 2 unit tests, 2 failed in 0m 6s
.Tests FAILED 
|
Error |
Forked Grails VM exited with error

It seems that controller is null in my tests. In the first test controller.index() is null. In the second test def model = controller.create() is not creating the object, then when I try to access model.roleInstance it cannot get the property.

Any idea would be appreciated. Thanks!

Upvotes: 1

Views: 681

Answers (1)

James Kleeh
James Kleeh

Reputation: 12228

Since you are using respond and not simply returning a map from the controller, you need to check the model property

def 'index action: 1 role'() {
    setup:
    Role roleInstance = new Role(roleName: "Role1").save()

    when:
    controller.index()

    then:
    model == [roleInstanceList: [roleInstance], roleInstanceTotal: 1]
}

I would suggest you read the documentation on testing controllers https://grails.github.io/grails-doc/2.5.x/guide/testing.html#unitTestingControllers

Upvotes: 2

Related Questions