2013-07-27 40 views

回答

2

我已经完成其如下:

1-使用FacesContextMocker类:​​

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

    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; 
    } 
} 

2-模拟OmniPartialViewContext对象如下:

FacesContext facesContext = FacesContextMocker.mockFacesContext(); 

     // mocking omnifaces OmniPartialViewContext to test Ajax.oncomplete 
     OmniPartialViewContext omniPartialViewContext = Mockito 
       .mock(OmniPartialViewContext.class); 
     Map<Object, Object> map = facesContext.getCurrentInstance() 
       .getAttributes(); 
     map.put(OmniPartialViewContext.class.getName(), omniPartialViewContext); 
     Mockito.when(facesContext.getCurrentInstance().getAttributes()) 
       .thenReturn(map); 
+0

事实上,我准备发表一条评论,说它只是用'OmniPartialViewCo存储为'FacesContext'属性ntext.class.getName()'作为键。 – BalusC

+0

@BalusC,感谢您的平时兴趣:D –