pmc*_*pmc 3 grails groovy spock geb
我想我需要在GebSpec测试中刷新hibernate会话,所以我想得到sessionFactory.
它看起来应该注入,但当我做这样的事情: -
class MySpec extends GebSpec {
def sessionFactory
...
def "test session"(){
....do some setup
then:
assert sessionFactory != null
}
Run Code Online (Sandbox Code Playgroud)
它失败,sessionFactory为null.
对我的问题的简短回答是 - 你为什么要这样做,这是一个功能测试,它可能远离正在运行的应用程序JVM.
原因是因为我想在Web内容发生时检查域对象是否已更新.Luke Daley亲切地指出你可以使用Remote-Control Grails插件(https://github.com/alkemist/grails-remote-control)来做到这一点,这非常酷.你安装插件然后去
assert remote {
MyDomainOb.findByName('fred') != null
}
Run Code Online (Sandbox Code Playgroud)
它发送该闭包在服务器上执行并返回结果,您可以测试.结果必须是可序列化的.
好的,这是一种方式 - 但是如果由于你强加的域模型而导致很多错综复杂的对象发生变化,那么远程插件有点难以获得测试结果.那么如何做你测试Hibernate的Session当你在同一个JVM?像这样:-
Session currentSession
def setup() {
ApplicationContext context = (ApplicationContext) ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT);
SessionFactory sf = context.getBean('sessionFactory')
currentSession = sf.getCurrentSession()
}
Run Code Online (Sandbox Code Playgroud)
将其添加到GebSpec的顶部,然后可以调用currentSession.clear()或currentSession.flush()
我需要让测试会话更新,所以currentsession.clear()就是答案.
请注意,如果被测目标位于单独的VM中,则无法帮助您,因此您必须使用远程.