Christian
Christian

Reputation: 737

Unit testing a controller method fails because view is null

I'm using Grails 2.2.0. This is my method to be tested:

def extendedSearchIndex () {
    log.debug("ExtendedSearchIndex ... ");
    def deviceClass = deviceService.getDeviceClass(request)
    if (deviceClass == "FeaturePhone") {
    render(view: '/featurephone/expanded_search')
    }
}

This is my test method:

void testExtendedSearchIndex01() {
    deviceServiceMock.demand.getDeviceClass(1..10) { def myRequest, boolean verbose ->
        return "FeaturePhone"
    }
    controller.deviceService = deviceServiceMock.createMock()
    controller.extendedSearchIndex()

    assert view == "/featurephone/expanded_search"
}

This test fails because view is null. But why is it null? Shouldn't it be /featurephone/expanded_search? Am I missing something?

Thanks for your help.

– Chris

Upvotes: 2

Views: 321

Answers (1)

user800014
user800014

Reputation:

You mocked a signature of the method getDeviceClass() that needs a def and a boolean, but your controller use another that needs only a def. I think that your mock should be:

deviceServiceMock.demand.getDeviceClass(1..10) { def myRequest ->
  return "FeaturePhone"
}

A suggestion is to use a String for deviceClass, since you know the type returned by getDeviceClass():

String deviceClass = deviceService.getDeviceClass(request)
if(deviceClass == "FeaturePhone")

And if you use an IDE, e.g. STS, you can debug your controller to check the value returned by the service.

Upvotes: 2

Related Questions