对服务进行Grails集成测试:无法在null对象上设置属性

时间:2015-01-19 12:51:59

标签: grails service nullpointerexception spock

我正在为我的应用程序使用Grails 2.4.4,并使用spock框架进行测试。

class TaxpayerService {
    static transactional = false

    def springSecurityService

    def setImporterDetails(TvfGen tvfGen) {
        if (isTrader()) {
            String taxPayerCode = springSecurityService?.principal?.getAt("tin")
            def importerDetails = HistorizationSupport.withHistorizedFinder(BusinessLogicUtils.getWorkingDate(tvfGen)) {
                Company.findByCode(taxPayerCode)
            }

            if (importerDetails) {
                tvfGen.with {
                    impTaxPayerAcc = taxPayerCode
                    impName = importerDetails.name
                }
            }
        }
    }
}

这是我的集成测试:

@Build([TvfGen])
class TaxpayerServiceSpec extends IntegrationSpec {
    def taxpayerService
    def springSecurityService

    void setup() {
    }

    void tearDown() {
        // Tear down logic here
    }

    void "test set importer details"() {
        given:
        def tvfGen = TvfGen.build()
        springSecurityService = [principal: [tin: '0815790B', authorities: [ROLE_TRADER]]]
        taxpayerService.springSecurityService = springSecurityService
        taxpayerService.setImporterDetails(tvfGen)

        expect:
        tvfGen.impName == 'OMNI VALUE'
    }
}

结果我收到错误:

Cannot set property 'springSecurityService' on null object
java.lang.NullPointerException: Cannot set property 'springSecurityService' on null object
    at TaxpayerServiceSpec.test set importer details(TaxpayerServiceSpec.groovy:34)

0 个答案:

没有答案
相关问题