executeUpdate query not working on grails spock te

2019-09-19 01:50发布

问题:

Now willing to do integration test as below but problem is that

MerchantTier.executeUpdate('update MerchantTier..........'), 

here update does not working

but if I make update with

def merchant = MerchantTier.get(params.id.toLong())

merchant.setValue(merchantTierVal)

instead of execute update it works

Is there is any prolem with executeUpdate Query?

def merchantTier

def setup() {

merchantTier = new MerchantTier(
     startTier: tier,
     endTier: tier,
     value: 2.02).save(flush: true)
}


void "for given merchantTierId update merchantTier"(){

     setup:
     params = [id:merchantTier.id,tierVal:2]

     when:
     testData = updateIndividualSuperResellerTier(params)

     then:"return data"
     merchantTier.value==params.tierVal
}

def updateIndividualSuperResellerTier(params) {

     def merchantTierVal = 0
     if (params.tierVal) {
         merchantTierVal = params.tierVal.toDouble()
     }
     def merchantTier = MerchantTier.get(params.id.toLong())
     def updateMerchantTier = MerchantTier.executeUpdate('update MerchantTier mt set mt.value=:mrValue where mt.id=:mtId', [mrValue: merchantTierVal, mtId: params.id.toLong()])
}

回答1:

There seems to be no problem with executeUpdate, the problem here could be, that executeUpdate did not return an object, it just return the number of rows updated so updateMerchantTier doesnot contain updatedObject.

Also you should again fetch the object as it is updated by executeUpdate in your void "for given merchantTierId update merchantTier"() then: statement

then:"return data"
 merchantTier.value==params.tierVal

here merchantTier is still an old object hence will not be having value equal to params.tierVal

in your other case you are using setter explicitly to set the property and hence it passed your Integration test.

 def merchant = MerchantTier.get(params.id.toLong())
 merchant.setValue(merchantTierVal)

hope this helps. Thanks