I have a problem with my Unit Test. This is my Unit Test:
void "Test the update action performs an update on a valid domain instance"() {
given:
def aclServiceMock = mockFor(AclService)
aclServiceMock.demand.updateRolePermissions { Role role, Map, roleMap, List permissions -> return true }
controller.aclService = aclServiceMock.createMock()
def roleInstance = setupRoleController()
mockCommandObject RoleCommand
RoleCommand cmd = new RoleCommand(
authority: roleInstance?.authority,
description: roleInstance?.description
)
when:"A valid domain instance is passed to the update action"
controller.update(roleInstance, cmd)
then:"A redirect is issued to the show action"
response.redirectedUrl == "/role/index"
flash.message != null
flash.error == null
}
And this is the result that I get from the terminal:
| Failure: Test the update action performs an update on a valid domain instance(ph.gov.doe.core.acl.RoleControllerSpec)
| Condition not satisfied:
flash.message != null
| | |
[:] null false
at ph.gov.doe.core.acl.RoleControllerSpec.Test the update action performs an update on a valid domain instance(RoleControllerSpec.groovy:222)
I can't seem to figure out why my unit test on that particular method keeps failing. Maybe my Command Object mock is the key? Thanks in advance.
EDIT: condition should be 'response.redirectedUrl == "/role/index"', I just copied the wrong message cause i was fiddling with the code at the time.
EDIT: This is the controller method:
def update(Role roleInstance, RoleCommand roleCommand) {
Map roleMap = [:]
ArrayList collectPermission = [], getPermissions = [], roleList = []
def savedRoleInstance
/** collect selected permission into arraylist */
collectPermission.addAll(params?.selectedPermission ?: [])
getPermissions = Permission.findAllByIdInList(collectPermission)
def roleEditInstance = Role.get(params?.roleId)
/** Set data for validation of Role */
roleCommand.with {
id = roleEditInstance?.id
authority = params?.authority
description = params?.description
}
roleCommand.validate()
/** Check if the set of permission already exists */
roleList = RolePermission.findAllByPermissionInListAndRoleNotEqual(getPermissions, Role.findByAuthority('ROLE_SUPERADMIN'))?.role
def duplicateRolePermission = roleList.find { r -> r.getAuthorities().sort { it?.id } == getPermissions.sort { it?.id } && r != roleEditInstance }
if (collectPermission.isEmpty()) {
flash.error = message(code: 'role.permissions.blank', args: [message(code: 'role.label', default: params?.authority)])
respond roleInstance, model:[roleList: Role.list(), permissionList: Permission.findAllByAuthorityNotEqual('PERM_DASHBOARD_VIEW'), inheritPermission: params?.inheritPermission, selectedPermission: getPermissions], view: "edit"
} else if (roleCommand.hasErrors() || duplicateRolePermission != null) {
bindData(roleCommand, roleInstance)
if(duplicateRolePermission != null){
flash.error = message(code: 'role.permissions.unique', args: [message(code: 'role.label', default: getPermissions?.description)])
}
respond roleCommand.errors, model:[roleInstance: roleCommand,roleList: Role.list(), permissionList: Permission.findAllByAuthorityNotEqual('PERM_DASHBOARD_VIEW'), inheritPermission: params?.inheritPermission, selectedPermission: getPermissions, roleId: roleEditInstance?.id], view: "edit"
} else {
/** Save the Role */
roleMap = [authority: params?.authority, description: params?.description]
def savedRole = aclService.updateRolePermissions(roleEditInstance, roleMap, getPermissions)
if (currentAccount) {
auditLogService.logEvent(currentAccount.emailAddress, "UPDATE_ROLE_SUCCESS", "Successfully updated role details.", true)
}
flash.message = message(code: 'role.updated.message', args: [message(code: 'role.label', default: savedRole?.authority)])
flash.id = savedRole?.id
redirect action: 'view', params:[id: savedRole?.id]
}
}
It seems that the app you're trying to test could be designed better. Lots of logic could be moved from controller to service (separation of concerns principle).
Also your test looks like integration-test rather then unit. So I would recommend you to get acquainted with interaction based testing.
Good luck!