本文介绍了嘲笑FacesContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图向JSF应用程序添加一些单元测试。这个应用程序并没有太依赖任何最佳实践,所以很多服务方法都使用 FacesContext 从托管会话bean中提取数据,如下所示:



(这是在util类中)
$ b $ pre $ public static Object getPageBean(String beanReference){
FacesContext fc = FacesContext.getCurrentInstance();
VariableResolver vr = fc.getApplication()。getVariableResolver();
return vr.resolveVariable(fc,beanReference);
}

最好的方法是什么?我正在使用groovy,所以我有更多的选项可以创建我无法正常创建的类。

解决方案

能够在纯粹的时髦中嘲笑它。
i提供了它可以返回的MockBeans的映射:

pre $ private $ FacesContext getMockFacesContext(def map){
def fc = [
getApplication:{
return [getVariableResolver:{
return [resolveVariable:{FacesContext fc,String name - >
return map [name]
}] as VariableResolver
}] as Application
},
addMessage:{String key,FacesMessage val - >
println添加了关键字:[$ {key}] value:[$ {val.getDetail()}]给JsfContext消息

getMessages:{return null}
]作为FacesContext;
返回fc;
}


I am trying to add some unit tests to a JSF application. This application didnt rely heavily on any best practices, so many service methods use the FacesContext to pull data from managed session beans like so:

(this is inside a util class)

  public static Object getPageBean(String beanReference) {
      FacesContext fc = FacesContext.getCurrentInstance();
      VariableResolver vr = fc.getApplication().getVariableResolver();
      return vr.resolveVariable(fc, beanReference);
  }

What would be the best way to mock this? I am using groovy so i have a few more options for creating classes that i cant normally create.

解决方案

in my case i was able to mock it in pure groovy.i provide a map of MockBeans that it can return:

private FacesContext getMockFacesContext(def map){
        def fc = [
          "getApplication": {
            return ["getVariableResolver": {
              return ["resolveVariable": { FacesContext fc, String name ->
                return map[name]
              }] as VariableResolver
            }] as Application
          },
          "addMessage": {String key, FacesMessage val ->
            println "added key: [${key}] value: [${val.getDetail()}] to JsfContext messages"
          },
          "getMessages": {return null}
        ] as FacesContext;
        return fc;
      }

这篇关于嘲笑FacesContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 01:08