2 Replies Latest reply on Jun 1, 2010 10:59 AM by Marcus Davis

    JUnit Testing : NullPointerException

    Marcus Davis Newbie

      Hello everyone. I'm quite new to JSF taglib development. I've developed a custom JSF taglib, which either disables of hides(rendered=false) if unauthorized user logs in. Thankfully, the taglib functions correctly and performs well.

      After developing, I started building Unit Testing codes with jUnit and Mockito. I haven't had much luck with that as I started facing an issue with the following method.


      public void setSecurityInterceptor(FacesContext context) {
              ApplicationContext appContext = (ApplicationContext)FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
              SecurityInterceptor securityInterceptor =
                  (SecurityInterceptor)appContext.getBean( SECURITY_INTERCEPTOR );
              this.securityInterceptor = securityInterceptor;



      When FacesContextUtils's  getWebApplicationContext(FacesContext) method flagged when it executes  this line:


      Object attr =  fc.getExternalContext().getApplicationMap().get(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);   (Line 51 of FacesContextUtil.java source)


      Following is the source code for  getCurrentInstance() method from  http://grepcode.com/file/repository.jboss.com/maven2/javax.faces/jsf-api/1.2_13/javax/faces/context/FacesContext.java


      In the unit testing class, I tried creating a mocked applicationContext  and then verify whether the process occurs by executing  setSecurityInterceptor method. Following error occurs:


          at org.springframework.web.jsf.FacesContextUtils.getWebApplicationContext(FacesContextUtils.java:51)


      So far I've tried using different ways of mocking  the FacesContext object, and using MockFacesContext class from  org.jboss.seam.mock as well as using Shale Mock. I haven't been too lucky with google as above approaches were all I've found so far from googling.


      I've also tried  creating a mock ThreadLocal and inserting/creating FacesContext instance  but wasn't successful since that's not the actual Thread I'm running my mocked objects on. Maybe my approach should be different. There  must be a way to test my Renderer.


      Do you happen to have any idea / suggestion on how to successfully complete unit testing for this method?