如何在withCriteria(Spock)中测试具有resultTransformer的grail服务。

时间:2015-05-21 06:05:12

标签: unit-testing grails spock

在下面的课程中,我只需要RevShareFormula.withCriteria来返回结果, 但是在resultTransformer()方法中获得异常。

任何人都可以告诉我如何模拟以下方法,以便从withCriteria获得一些结果

这是班级:

class PartnerFinancialService {

def getPartnerPayeeRevenuShareDetails(long partnerPayeeId, def contextTypeCode) {

def partnerPayeesRevShareFormula = RevShareFormula.withCriteria {
  resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
  createAlias('partnerRevShareConfig', 'partnerRevShareConfig')
  createAlias('pricingModel', 'pricingModel')
  createAlias('partnerRevShareConfig.revshareCategory', 'revshareCategory')
  and {
    eq("revshareCategory.payeeProfileId", partnerPayeeId)
    eq("revshareCategory.referenceContextTypeCode", contextTypeCode)
    isNull("partnerRevShareConfig.revshareValidToDate")
  }

  projections {
    property("id", "formulaId")
    property("pricingModel.id", "pricingModelId")
    property("pricingModel.pricingName", "pricingName")
    property("pricingModel.pricingType", "pricingType")
    ..
    ..
   }
}    
}   

这是测试类

@TestFor(PartnerFinancialService)
@Mock(RevShareFormula)
class PartnerFinancialServiceSpec extends Specification {

void "test getPartnerPayeeRevShareDetails"() {
def partnerPayeeRevShare = new PartnerRevShareConfig()
    partnerPayeeRevShare.id = 1
    def revShareModel = new PricingModel();
    revShareModel.id = 1
    def partnerPayeeRevShareFormula = new RevShareFormula();
    partnerPayeeRevShareFormula.id=5
    partnerPayeeRevShareFormula.pricingModel = revShareModel
    partnerPayeeRevShareFormula.partnerRevShareConfig = partnerPayeeRevShare
    partnerPayeeRevShareFormula.revshareFormula = "revshare*10"
    partnerPayeeRevShareFormula.revshareTierHighValue = 0
    partnerPayeeRevShareFormula.revshareTierLowValue= 0 

    RevShareFormula.metaClass.static.withCriteria = {partnerPayeeRevShareFormula}

    when:
    def result = service.getPartnerPayeeRevenuShareDetails(1,"PKG")
    then:
    //assert result.pricingModel.id == 1
    println "Succesfully Fetched from DB"
} 
}

获得以下异常。

<testcase classname="com.orbitz.dat.partners.PartnerFinancialServiceSpec" name="test getPartnerPayeeRevShareDetails" time="0.039">
<error message="No signature of method: com.orbitz.dat.partners.PartnerFinancialService.resultTransformer() is applicable for argument types: (org.hibernate.transform.AliasToEntityMapResultTransformer) values: [org.hibernate.transform.AliasToEntityMapResultTransformer@3632aa4]" type="groovy.lang.MissingMethodException">groovy.lang.MissingMethodException: No signature of method: com.orbitz.dat.partners.PartnerFinancialService.resultTransformer() is applicable for argument types: (org.hibernate.transform.AliasToEntityMapResultTransformer) values: [org.hibernate.transform.AliasToEntityMapResultTransformer@3632aa4]
    at com.orbitz.dat.partners.PartnerFinancialService.$tt__getPartnerPayeeRevenuShareDetails_closure24(PartnerFinancialService.groovy:39)
    at grails.gorm.CriteriaBuilder.invokeClosureNode(CriteriaBuilder.java:1093)
    at grails.gorm.CriteriaBuilder.invokeMethod(CriteriaBuilder.java:314)
    at org.grails.datastore.gorm.GormStaticApi.withCriteria_closure11(GormStaticApi.groovy:304)
    at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302)
    at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:37)
    at org.grails.datastore.gorm.GormStaticApi.withCriteria(GormStaticApi.groovy:303)
    at com.orbitz.dat.partners.PartnerFinancialService.$tt__getPartnerPayeeRevenuShareDetails(PartnerFinancialService.groovy:38)
    at com.orbitz.dat.partners.PartnerFinancialServiceSpec.test getPartnerPayeeRevShareDetails(PartnerFinancialServiceSpec.groovy:71)

1 个答案:

答案 0 :(得分:0)

使用集成测试。切勿使用单元测试来测试ORM代码。我知道它的速度较慢而且体验不那么令人愉快,但如果您认为自己实际上正在使用此测试测试与数据库查询相关的内容,那么您就是在愚弄自己。您正在测试grails的测试框架(内存中的GORM实现)