如何在Grails Geb / Spock测试用例中获得sessionFactory? [英] How do you get the sessionFactory in a Grails Geb/Spock test case?

查看:123
本文介绍了如何在Grails Geb / Spock测试用例中获得sessionFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我需要在GebSpec测试中刷新hibernate会话,所以我想获得sessionFactory。



它看起来应该被注入,但是当我做这样的事情: - $ / $>

  class MySpec扩展GebSpec {
def sessionFactory
...
deftest session(){
....做一些设置
然后:
assert sessionFactory!= null
}

它会因sessionFactory为空而失败。

解决方案

我的问题的简短答案是 - 为什么你要这么做,这是一个功能测试,它可能远离正在运行的应用程序JVM。



为什么是因为我想检查当网络事件发生时域对象已经更新。 Luke Daley善意地指出,您可以使用Remote-Control Grails插件( https:// github.com/alkemist/grails-remote-control ),这非常酷。你安装插件然后去

  assert remote {
MyDomainOb.findByName('fred')!= null





$ b

然后发送该闭包在服务器上执行并返回结果,可以测试。结果必须是可序列化的。



好吧,这是一种方法 - 但是如果你有很多错综复杂的对象改变,远程插件有点难以得到测试结果由于强加在你身上的领域模型而引起的。那么当你在同一个JVM上时,你如何测试hibernate会话?像这样: -

pre $ Session currentSession

def setup(){
ApplicationContext context = (ApplicationContext)ServletContextHolder.getServletContext()。getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT);
SessionFactory sf = context.getBean('sessionFactory')
currentSession = sf.getCurrentSession()
}

将其添加到您的GebSpec的顶部,然后您可以调用currentSession.clear()或currentSession.flush()



I需要进行测试会话更新,因此currentsession.clear()就是答案。



注意,如果被测目标位于单独的虚拟机中,所以你必须使用远程。


I think I need to flush the hibernate session in a GebSpec test, and so I want to get the sessionFactory.

It looks like it should be injected but when I do something like this:-

class MySpec extends GebSpec {
def sessionFactory
...
def "test session"(){
....do some setup
then: 
  assert sessionFactory != null
}

it fails with sessionFactory being null.

解决方案

The short answer to my question is - why do you want to do that, it's a functional test and it may be remote from the running apps JVM.

The why is because I want to check that domain objects have been updated when web stuff happens. Luke Daley kindly pointed out that you can do that using the Remote-Control Grails plug in ( https://github.com/alkemist/grails-remote-control ) which is pretty cool. You install the plugin then go

 assert remote {
  MyDomainOb.findByName('fred') != null
 }

And it sends that closure to be executed on the server and returns the result, which you can test. The result must be serialisable.

OK so that's one way - but the remote plugin is a little harder to get results back for tests if you have a lot of intricate objects changing under the hood due to a domain model that is imposed on you. So how do you get the tests hibernate session when you are on the same JVM?? Like this:-

Session currentSession

    def setup() {
        ApplicationContext context = (ApplicationContext) ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT);
        SessionFactory sf = context.getBean('sessionFactory')
        currentSession = sf.getCurrentSession()
    }

Add that to the top of your GebSpec and then you can call currentSession.clear() or currentSession.flush()

I needed to have the tests session update so currentsession.clear() was the answer.

Note this won't help you if the target under test is in a separate VM, so you'll have to use remote.

这篇关于如何在Grails Geb / Spock测试用例中获得sessionFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆