gpt4 book ai didi

jsf-2 - 如何在junit中使用OmniFaces

转载 作者:行者123 更新时间:2023-12-01 15:00:29 25 4
gpt4 key购买 nike

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

关于jsf-2 - 如何在junit中使用OmniFaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17897127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com