繁体   English   中英

我如何在服务内部的域中模拟get方法grails

[英]How i can mock get method in Domain inside service grails

我尝试运行服务的规范测试,但是运行测试时,在获得UnitMeasure Domain的get方法后收到nullpointer。

我试图模拟这种静态方法,但是不起作用

我写这段代码:

我的服务是ValidationService:

class ValidationService {
    def validateLogistic(def logisticComposition,def logisticCompositionChild, def json, def params) {
        validateNetWeightAndNetWeightPallet(logisticComposition)
    }

    def test(def logisticComposition) {

    if(logisticComposition?.netWeightUnitMeasure?.conversionFactor > 0) {

    UnitMeasure netWeightUnitMeasure = UnitMeasure.get(logisticComposition.netWeightUnitMeasure.id)

    if(netWeightUnitMeasure.conversionFactor != null)
        throw new LogisticValidationException("Invalid field value")
}}

我的测试是ValidationServiceSpec由Specification扩展

@TestFor(ValidationService)
@Mock(LogisticComposition)
class ValidationServiceSpec extends Specification{
    def service
    JSONObject json

    def setup() {
        service = new ValidationService()
        json = new JSONObject()
        json.addLevel = true
    }

    def cleanup() {
    }


    void "validate logistic when net weigth master more than net weigth pallet"() {
        setup :
            UnitMeasure measure = new UnitMeasure()
            measure.conversionFactor = 1
            measure.abbreviation = "GR"
            measure.id = 1

            GrailsMock mockLocation = mockFor(UnitMeasure)
            mockLocation.demand.static.get() { int id -> return measure;
            }

        when:
            LogisticComposition logi = new LogisticComposition();
            logi.volume = new BigDecimal(100)
            logi.volumeUnitMeasure = measure;
            logi.volumeUnitMeasure.id = 1
            logi.gtin = "999999"
            logi.packing = new Packing()
            logi.amount = 1
            logi.height = new BigDecimal(100)
            logi.heightUnitMeasure = measure

            logi.width = new BigDecimal(100)
            logi.widthUnitMeasure = measure

            logi.depth = new BigDecimal(100)
            logi.depthUnitMeasure = measure

            logi.netWeight = new BigDecimal(100)
            logi.netWeightUnitMeasure = measure

            logi.netWeight = new BigDecimal(1000)
            logi.netWeightUnitMeasure = measure

            logi.netWeightPallet = new BigDecimal(100)
            logi.netWeightPalletUnitMeasure = measure 

            def params = new HashMap()
            params.addLevel = false

            service.test(logi)
        then:
            thrown LogisticValidationException
    }

错误:

   Running without daemon...
| Compiling 1 source files
| Compiling 1 source files.
| Running 2 unit tests...
| Running 2 unit tests... 1 of 2
| Failure:  validate logistic when net weigth master more than net weigth pallet(br.com.itemone.ValidationServiceSpec)
|  Expected exception of type 'br.com.itemone.LogisticValidationException', but got 'java.lang.NullPointerException'
    at org.spockframework.lang.SpecInternals.checkExceptionThrown(SpecInternals.java:79)
    at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:66)
    at br.com.itemone.ValidationServiceSpec.validate logistic when net weigth master more than net weigth pallet(ValidationServiceSpec.groovy:70)
Caused by: java.lang.NullPointerException: Cannot get property 'conversionFactor' on null object
    at br.com.itemone.ValidationService.test(ValidationService.groovy:35)
    at br.com.itemone.ValidationServiceSpec.validate logistic when net weigth master more than net weigth pallet(ValidationServiceSpec.groovy:68)
| Completed 1 unit test, 1 failed in 0m 3s
| Tests FAILED  - view reports in 

我怎样才能模拟这种方法? 有任何想法吗?

由于您要在ValidationServiceSpec中测试ValidationService,因此Grails框架会在单元测试中自动注入服务。

@TestFor(ValidationService)
@Mock(LogisticComposition)
class ValidationServiceSpec extends Specification{
// def service
JSONObject json

def otherService          // if you need to use other service in this unit test

// only if this service has dependency injection with other service
static doWithSpring = {
    otherService(OtherService)
}

def setup() {
    //service = new ValidationService()

    // this is not required if you do not call methods on this service within this unit test file
    otherService = Holders.grailsApplication.mainContext.getBean("otherService")
    json = new JSONObject()
    json.addLevel = true
}

def cleanup() {
}


void "validate logistic when net weigth master more than net weigth pallet"() {
    setup :
        UnitMeasure measure = new UnitMeasure()
        measure.conversionFactor = 1
        measure.abbreviation = "GR"
        measure.id = 1

        GrailsMock mockLocation = mockFor(UnitMeasure)
        mockLocation.demand.static.get() { int id -> return measure;
        }

    when:
        LogisticComposition logi = new LogisticComposition();
        ...
        service.test(logi)        // this is automatically injected
    then:
        thrown LogisticValidationException
}

如果您不想深入了解(GORM)模拟,则可以使用简单的groovy的元编程:

UnitMeasure.metaClass.static.get = { long id -> new UnitMeasure(...) }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM