I'd like to test one of my Beans, but it uses a FacesContext.
As testing, a JSF is not active of course, and therefore getting:
javax.enterprise.context.ContextNotActiveException: FacesContext is not active at util.locale.FacesContextProducer.getFacesContext(FacesContextProducer.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
But how can I do integration testing with facescontext? Do I have to mock the context? (or would I be going away from IT with this?)
Well the answer depends on what class you are testing, how the FacesContext is referenced by this class, and how the FacesContext instance is provided to this class.
You could use JSFUnit if your test fixture involves the issuance of a HTTP request to the server; in the case of JSFUnit, HtmlUnit is used to issue requests. Using JSFUnit will allow you to obtain a reference to the FacesContext instance, in the context of a request.
Alternatively, if your test fixture does not issue a HTTP request, you can create your own FacesContext implementation. As part of your test fixture, you can set the FacesContext instance for the current thread to an instance of this custom FacesContext class; the FacesContext.setCurrentInstance(...) API method allows you to do this.
|Retrieving data ...|