Grails 3集成测试将“找不到当前线程的会话”休眠异常时保存实体 [英] Grails 3 integration tests throw "No Session found for current thread" hibernate exception when saving entities
问题描述
我有以下grails规范测试
@Integration
@Rollback
class WebAppointmentControllerSpec extends Specification {
诊所
执业医师
患者
用户;
地址1
def setup(){
this.address1 =新地址(地点:'Calgary',administrativeArea:'Alberta',country:Country.findByCca2('CA '),postalCode:'T1A1A1',addressLine1:123 Tooth Street SW)
address1.save(failOnError:true)
this.clinic = new诊所(名称:'Test Clinic 1',地址:address1,primaryEmail:'test@clinic1.com',primaryPhone:phone1,网站:'http://www.clinic1.com',timeZone:DateTimeZone.forID('America / Edmonton'))
clinic。 save(failOnError:true)
this.practitioner = new Practitioner(admin:false,firstName:'John',lastName:'Smith',clinic:this.clinic,primaryTelephone:StringUtil.formatTelephoneNumber('1234567890'))
practitioner.save(failOnError:true)
user = new User(email:'user12345@appreciado.com',passwordHash:BCryptUtil.hashpw('testtest',BCryptUtil.gensalt(10)),enabled :true,accountE xpired:false,accountLocked:false,credentialsExpired:false,secretKey:new SecretKey(value:'dRZbYLVN = yjBW17V09Bf'),emailVerified:true,acceptTermsAndConditions:true,acceptPrivacyTerms:true).save(failOnError:true)
this .patient = new Patient(user:user,firstName:'User2',lastName:'User2Last',primaryTelephone:'+14035551212',dateOfBirth:StringUtil.parseRegistrationDate(1979/02/02),group:patientGroup,points: 200).save(failOnError:true)
}
def cleanup(){
}
匿名用户无法更新预约状态(){
给定:
RestBuilder休息=新RestBuilder()
预约预约=新预约(患者:this.patient,从业者:this.practitioner,clinic:this.clinic ,dateAndTime:LocalDateTime.parse('2015-03-15T10:00:00'),endDateAndTime:LocalDateTime.parse('2015-03-15T10:30:00'),lastUpdatedBy:this.practitioner)
当遇到主页时,会出现以下信息:
appointment.save()
RestResponse restResponse = rest.post(http://本地主机:8080 / API / V1 /移民/ $ arrivalid /状态);
then:发送401错误
restResponse.status == 401
}
}
运行时出现以下错误...
org.hibernate.HibernateException:No Session找到当前线程
$ pre $ at org.grails.orm.hibernate.GrailsSessionContext.currentSession(GrailsSessionContext.java:117)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at org.grails.orm.hibernate.SessionFactoryProxy.getCurrentSession(SessionFactoryProxy.java:148)
at org.grails。 orm.hibernate.HibernateSession.createQuery(HibernateSession.java:154)
at org.grails.orm.hibernate.HibernateSession.createQuery(HibernateSession.java:148)
at org.grails.datastore.gorm。 finder.AbstractFindByFinder.buildQuery(AbstractFindByFinder.java:39)
at org.grails.datastore.gorm.finders.AbstractFindByFinder $ 1.doInSession(AbstractFindByFinder.java:24)
at org.grail s org.grails.datastore.gorm.finders.AbstractFinder.execute上的
(AbstractFinder.java:41)$ or $ $
。 grails.datastore.gorm.finders.AbstractFindByFinder.doInvokeInternal(AbstractFindByFinder.java:22)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:156)
at org。 grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:356)
at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:112)
at org.grails。 datastore.gorm.internal.StaticMethodInvokingClosure.call(StaticMethodInvokingClosure.groovy:32)
at com.appreciado.server.api.WebAppointmentControllerSpec.setup(WebAppointmentControllerSpec.groovy:36)
有没有什么办法可以为我的集成测试启用一个hibernate会话?
目前,您无法将实体保存在 setup
方法中,并且应该(例如 setupData
),并在给定块的中直接从你的测试方法中调用它。
这个问题在即将发布的3.0.10版本中得到解决: https://github.com/grails/grails-core/commit/cc2f2ed86d18fded831242d0c8d2d1ee885fc8a5
I have the following grails Specification test
@Integration
@Rollback
class WebAppointmentControllerSpec extends Specification {
Clinic clinic
Practitioner practitioner
Patient patient
User user;
Address address1
def setup() {
this.address1 = new Address(locality: 'Calgary', administrativeArea: 'Alberta', country: Country.findByCca2('CA'), postalCode: 'T1A1A1', addressLine1: "123 Tooth Street SW")
address1.save(failOnError: true)
this.clinic = new Clinic(name: 'Test Clinic 1', address: address1, primaryEmail: 'test@clinic1.com', primaryPhone: phone1, website: 'http://www.clinic1.com', timeZone: DateTimeZone.forID('America/Edmonton'))
clinic.save(failOnError: true)
this.practitioner = new Practitioner(admin: false, firstName: 'John', lastName: 'Smith', clinic: this.clinic, primaryTelephone: StringUtil.formatTelephoneNumber('1234567890'))
practitioner.save(failOnError: true)
user = new User(email: 'user12345@appreciado.com', passwordHash: BCryptUtil.hashpw('testtest', BCryptUtil.gensalt(10)), enabled: true, accountExpired: false, accountLocked: false, credentialsExpired: false, secretKey: new SecretKey(value: 'dRZbYLVN=yjBW17V09Bf'), emailVerified: true, acceptTermsAndConditions: true, acceptPrivacyTerms: true).save(failOnError: true)
this.patient = new Patient(user: user, firstName: 'User2', lastName: 'User2Last', primaryTelephone: '+14035551212', dateOfBirth: StringUtil.parseRegistrationDate("1979/02/02"), group: patientGroup, points: 200).save(failOnError: true)
}
def cleanup() {
}
void "Anonymous user cannot update appointment status"() {
given:
RestBuilder rest = new RestBuilder()
Appointment appointment = new Appointment(patient: this.patient, practitioner: this.practitioner, clinic: this.clinic, dateAndTime: LocalDateTime.parse('2015-03-15T10:00:00'), endDateAndTime: LocalDateTime.parse('2015-03-15T10:30:00'), lastUpdatedBy: this.practitioner)
appointment.save(failOnError: true)
appointment.save()
when: "The home page is visited"
RestResponse restResponse = rest.post("http://localhost:8080/api/v1/arrivals/$arrivalid/status");
then: "A 401 error is sent"
restResponse.status == 401
}
}
When it runs I get the following error...
org.hibernate.HibernateException: No Session found for current thread
at org.grails.orm.hibernate.GrailsSessionContext.currentSession(GrailsSessionContext.java:117)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at org.grails.orm.hibernate.SessionFactoryProxy.getCurrentSession(SessionFactoryProxy.java:148)
at org.grails.orm.hibernate.HibernateSession.createQuery(HibernateSession.java:154)
at org.grails.orm.hibernate.HibernateSession.createQuery(HibernateSession.java:148)
at org.grails.datastore.gorm.finders.AbstractFindByFinder.buildQuery(AbstractFindByFinder.java:39)
at org.grails.datastore.gorm.finders.AbstractFindByFinder$1.doInSession(AbstractFindByFinder.java:24)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302)
at org.grails.datastore.gorm.finders.AbstractFinder.execute(AbstractFinder.java:41)
at org.grails.datastore.gorm.finders.AbstractFindByFinder.doInvokeInternal(AbstractFindByFinder.java:22)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:156)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:356)
at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:112)
at org.grails.datastore.gorm.internal.StaticMethodInvokingClosure.call(StaticMethodInvokingClosure.groovy:32)
at com.appreciado.server.api.WebAppointmentControllerSpec.setup(WebAppointmentControllerSpec.groovy:36)
Is there some way I can enable a hibernate session for my integration test?
Currently you cannot save entities in the setup
method and should rename the method something else (setupData
for example) and call it directly from your test method in the given
block.
This is fixed in the soon to be released 3.0.10 version:https://github.com/grails/grails-core/commit/cc2f2ed86d18fded831242d0c8d2d1ee885fc8a5
这篇关于Grails 3集成测试将“找不到当前线程的会话”休眠异常时保存实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!