2013-07-27 68 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 –