2 Replies Latest reply on Mar 6, 2012 4:07 AM by david meier

    FacesContext is not active?

    david meier Newbie



      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?)



        • 1. Re: FacesContext is not active?
          Vineet Reynolds Apprentice

          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.

          • 2. Re: FacesContext is not active?
            david meier Newbie

            I'm using FacesContext to obtain the locale in the bean I'm testing.

            Will have a look at JSFUnit. ty!