如何在我的Prosper规范中创建WCMUsePojo的实例? [英] How to create instance of WCMUsePojo in my Prosper spec?

查看:192
本文介绍了如何在我的Prosper规范中创建WCMUsePojo的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能正常的WCMUsePojo Groovy类,它从一个幽默的html组件中调用。我试图根据 Prosper 设置方法的内容创建一个WCMUsePojo类的实例进行测试。

I have a functioning WCMUsePojo Groovy class which is called from a sightly html component. I am trying to create an instance of my WCMUsePojo class for testing based on the content from the Prosper setup method.

它基本上与但我没有看到它的答案这特别是关于如何在Prosper框架内的WCMUsePojo类中单元测试方法。是否有Java或Groovy等同于Sightly的数据适用属性?

It's basically the same type of question as How can I create an instance of WCMUsePojo in a servlet for a specific page? But I don't see it answered and this is specifically about how to unit test methods in WCMUsePojo classes within the Prosper framework. Is there a Java or Groovy equivalent to Sightly's data-sly-use attribute?

def setupSpec() {
    pageBuilder.content {
        page_with_new_gridwrapper {
            'jcr:content'{
                'gridpar' {
                    'my_gridwrapper'('sling:resourceType':'my/components/my_gridwrapper') {

                    }
                }
            }
        }
    }
}

def "Test Page with New Grid Container"(){
    Page page = pageManager.getPage("/content/page_with_new_gridwrapper")

 // the following 2 lines return null :-( 
 // but I would prefer these to return an object with the type MyGridContainerHelper
    MyGridContainerHelper cmp = page.getContentResource().getChild("gridpar/my_gridwrapper").adaptTo(MyGridContainerHelper.class)
    Component cmp2 = WCMUtils.getComponent(page.getContentResource().getChild("gridpar/my_gridwrapper"))

    expect:
    page != null //passes
    page.getContentResource().getChild("gridpar/my_gridwrapper") != null //passes
    cmp != null // fails
    cmp2 != null // fails
    cmp.resourceType == "my/components/my_gridwrapper" // fails

}


推荐答案

我结束实例化对象并调用init方法传递在包含正在测试的资源的SimpleBindings对象中。这似乎适用于我的目的。

I ended up instantiating the object and calling the init method passing in a SimpleBindings object containing the resource I was testing with. This seems to work well for my purposes.

MyGridContainerHelper gridContainer = new MyGridContainerHelper();
SimpleBindings bindings = new SimpleBindings()
bindings.put("resource", page.getContentResource().getChild("gridpar/my_gridwrapper"))
gridContainer.init(bindings)

这篇关于如何在我的Prosper规范中创建WCMUsePojo的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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