How to mock FacesContext - getRequestParameterMap

1.4k views Asked by At

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()
1

There are 1 answers

0
centic On

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.