如何模拟 FacesContext - getRequestParameterMap
我有一个类,我使用 getRequestParameterMap 来检索一些数据,如下所示:
FacesContext fc = FacesContext.getCurrentInstance();
String oidValue = fc.getExternalContext.getRequestParameterMap().get("oidValue");
我需要创建一些 Junit 测试,因为涉及一些条件,所以我正在寻找某种方法来模拟值:
getRequestParameterMap()
I have a class that I use a getRequestParameterMap to retrieve some data, like this:
FacesContext fc = FacesContext.getCurrentInstance();
String oidValue = fc.getExternalContext.getRequestParameterMap().get("oidValue");
And I need to create some Junit tests because there is some conditionals involved, so what I'm looking for is some way to mock values on:
getRequestParameterMap()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您会为 FacesContext 使用一个模拟对象,并在调用 getExternalContext() 时为ExternalContext 返回另一个模拟对象,以便您最终可以返回一个包含此测试所需值的映射。然而
这里的问题是FacesContext和ExternalContext都是抽象类而不是接口,这导致easymock无法模拟它们。
然而,有PowerMock,它可以做一些增强的事情,其中包括调整现有类的字节码,它应该能够完成您在这里尝试做的事情。
Usually you would use a mock-object for the FacesContext and return another mock-object for ExternalContext when getExternalContext() is called so that you finally can return a map with the values necessary for this test. However
The problem here is that FacesContext and ExternalContext both are abstract classes instead of interfaces, which causes easymock to fail to mock these.
However there is PowerMock, which can do some enhanced things, among others adjust bytecode of existing classes, it should be able to do what you are trying to do here.