首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模拟FacesContext

模拟FacesContext
EN

Stack Overflow用户
提问于 2010-11-23 10:57:42
回答 7查看 25K关注 0票数 19

我正在尝试向JSF应用程序添加一些单元测试。该应用程序不太依赖于任何最佳实践,因此许多服务方法使用FacesContext从托管会话beans中提取数据,如下所示:

(这是在一个util类中)

代码语言:javascript
复制
  public static Object getPageBean(String beanReference) {
      FacesContext fc = FacesContext.getCurrentInstance();
      VariableResolver vr = fc.getApplication().getVariableResolver();
      return vr.resolveVariable(fc, beanReference);
  }

模拟这种情况最好的方式是什么?我使用的是groovy,所以我有更多的选择来创建我不能正常创建的类。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-12-02 22:13:18

在我的例子中,我可以用纯groovy来模拟它。我提供了一个它可以返回的MockBeans地图:

代码语言:javascript
复制
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;
      }
票数 2
EN

Stack Overflow用户

发布于 2012-01-27 01:11:50

您可以在运行测试之前调用setCurrentInstance(FacesContext),通过FacesContext.getCurrentInstance返回模拟上下文。该方法是受保护的,但您可以通过反射或扩展FacesContext来访问它。这里有一个使用Mockito here的示例实现。

票数 16
EN

Stack Overflow用户

发布于 2014-02-27 05:23:44

这个网址提供了一篇关于它的非常好的文章:http://illegalargumentexception.blogspot.com/2011/12/jsf-mocking-facescontext-for-unit-tests.html

您有了托管bean:

代码语言:javascript
复制
 package foo;

import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class AlphaBean {
  public String incrementFoo() {
    Map<String, Object> session = FacesContext.getCurrentInstance()
        .getExternalContext()
        .getSessionMap();
    Integer foo = (Integer) session.get("foo");
    foo = (foo == null) ? 1 : foo + 1;
    session.put("foo", foo);
    return null;
  }
}

您可以将FacesContext存根:

代码语言:javascript
复制
package foo.test;

import javax.faces.context.FacesContext;

import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

public abstract class ContextMocker extends FacesContext {
  private ContextMocker() {
  }

  private static final Release RELEASE = new Release();

  private static class Release implements Answer<Void> {
    @Override
    public Void answer(InvocationOnMock invocation) throws Throwable {
      setCurrentInstance(null);
      return null;
    }
  }

  public static FacesContext mockFacesContext() {
    FacesContext context = Mockito.mock(FacesContext.class);
    setCurrentInstance(context);
    Mockito.doAnswer(RELEASE)
        .when(context)
        .release();
    return context;
  }
}

然后编写你的单元测试:

代码语言:javascript
复制
@Test
  public void testIncrementFoo() {
    FacesContext context = ContextMocker.mockFacesContext();
    try {
      Map<String, Object> session = new HashMap<String, Object>();
      ExternalContext ext = mock(ExternalContext.class);
      when(ext.getSessionMap()).thenReturn(session);
      when(context.getExternalContext()).thenReturn(ext);

      AlphaBean bean = new AlphaBean();
      bean.incrementFoo();
      assertEquals(1, session.get("foo"));
      bean.incrementFoo();
      assertEquals(2, session.get("foo"));
    } finally {
      context.release();
    }
  }
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4252353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档