我正在为我的应用程序使用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)