2
我希望能夠調用Ajax.oncomplete
在內部做出呼叫:如何在我的測試方法,使用OmniFaces在JUnit
OmniPartialViewContext.getCurrentInstance
請告知如何做到這一點。
我希望能夠調用Ajax.oncomplete
在內部做出呼叫:如何在我的測試方法,使用OmniFaces在JUnit
OmniPartialViewContext.getCurrentInstance
請告知如何做到這一點。
我已經完成其如下:
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);
事實上,我準備發表一條評論,說它只是用'OmniPartialViewCo存儲爲'FacesContext'屬性ntext.class.getName()'作爲鍵。 – BalusC
@BalusC,感謝您的平時興趣:D –