Grails GORM服务类测试withNewSession()

bon*_*ond 2 grails integration-testing hibernate grails-orm

我试图更新我的测试,因为我对我的服务类进行了更改.在我添加的服务方法中("withNewSession")

Domain.withNewSession {
  .. ...
  domain.save()
}
Run Code Online (Sandbox Code Playgroud)

因为我正在使用"withNewSession",所以我得到方法缺少异常,因为这个域在我的测试类中被模拟.

小智 5

单元测试不允许使用withNewSession方法.在测试类中使用:

@org.junit.Before
void before() {
    DomainClazz.metaClass.static.withNewSession = {Closure c -> c.call() }
}
Run Code Online (Sandbox Code Playgroud)