gpt4 book ai didi

hibernate - Grails 3 集成测试在保存实体时抛出 "No Session found for current thread" hibernate 异常

转载 作者:行者123 更新时间:2023-12-01 00:46:32 25 4
gpt4 key购买 nike

我有以下 grails 规范测试

@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
}
}

当它运行时,我收到以下错误...

org.hibernate.HibernateException: 找不到当前线程的 session
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)

有什么方法可以为我的集成测试启用 hibernate session 吗?

最佳答案

目前您无法在 setup 中保存实体。方法,并且应该将方法重命名为其他名称(例如 setupData)并直接从 given 中的测试方法中调用它堵塞。

这在即将发布的 3.0.10 版本中得到修复:https://github.com/grails/grails-core/commit/cc2f2ed86d18fded831242d0c8d2d1ee885fc8a5

关于hibernate - Grails 3 集成测试在保存实体时抛出 "No Session found for current thread" hibernate 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34034315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com