如何在junit中使用OmniFaces



在我的测试方法中,我希望能够调用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-mock 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);

相关内容

  • 没有找到相关文章

最新更新