Unit testing Grails controller

how to preform unit test this function

private void secondaryRepError(def secRepCmd) {

    def repProfile = springSecurityService?.currentUser?.representativeProfile
    def productTypes = representativeManagementService.getAssignedProductTypes(repProfile)
    def facilities = representativeManagementService.getAssignedFacilities(repProfile)
    def facilityType = representativeManagementService.getFacilityType()
    def listOfRetail   = representativeManagementService.listOfFacilities(facilities?.toList(), facilityType?.retail)
    def listOfLpg      = representativeManagementService.listOfFacilities(facilities?.toList(), facilityType?.lpg)
    def listOfPipeline = representativeManagementService.listOfFacilities(facilities?.toList(), facilityType?.pipeline)
    def listOfRelated  = representativeManagementService.listOfFacilities(facilities?.toList(), facilityType?.related)
    render view:'addSecondaryRep', model: [secondaryRepInstance: secRepCmd, appType: ApplicationType.getAll(), productTypes: productTypes, facilities: facilities, retailFacility: facilityType?.retail, lpgFacility: facilityType?.lpg, pipelineFacility: facilityType?.pipeline, facFacility: facilityType?.related, listOfRetail: listOfRetail, listOfLpg: listOfLpg, listOfPipeline: listOfPipeline, listOfRelated:listOfRelated]


  }

Upvotes: 0

Views: 33

Answers (1)

Adeel Ansari
Adeel Ansari

Reputation: 39907

I would recommend to just test all the required service method invocations, using mocks -- not the results of those invocation, otherwise your test depends on those method. Those service methods should have their own tests.

Moreover, test whether the view is correct or not, and model has all the required properties.

Refer, Grails - Unit Testing Controllers.

Docs, will help you in getting current view and model.

Command, grails create-unit-test, grails create-unit-test <your-controller-here>, will generate the test class for you to start with.

Upvotes: 1

Related Questions