在我的测试方法中,我希望能够调用Ajax.oncomplete
内部调用:
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);