save() throws NULL POINTER EXCEPTION during UNIT TEST ... HELP!!

antonio

New member
UNIT TEST ERROR
An update action calls save on a domain object DataCenter that has been mocked. All methods like .get(), .hasErrors() work on the DataCenter during the test but the save() method throws NullPointerException.

Can anyone tell me why this problem is occurring and how to solve it?

Heres a sniplet from the code:

<--------------------------ACTION BEING TESTED------------------------------->
def myaction = {
def Instance = data.get( params.id )
if(Instance) {
if(!Instance.hasErrors()) {
Instance.save(flush:true)

flash.message = "Instance ${params.id} updated"
redirect(action:show,id:Instance.id)
}
}
}
<------------------------------------------------------------------------------>

<--------------------------TEST CODE USED----------------------------------->
void testActionWhenInstanceFoundSaveSuccess() {
def testInstances = [ new Instance('InstanceId': 1, 'name':"I_1", 'city': "TEST_CITY" ) ]
mockDomain(Instance,testInstances)
this.controller.params.id = 1
this.controller.myaction()//Fails Here
}

<----------------------------------------------------------------------------->
 
Last edited:

WillK

New member
Hi antonio-

I'm not practiced in mockDomain for Grails controller test, however,
it may be as simple as the 'InstanceId': 1 needing to be written 'instanceId':1.
If it is not as simple as that perhaps you could post the listing of the entire Controller Unit Test class?
Will
 
Top