You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by remnant <pa...@gmail.com> on 2010/05/27 18:25:32 UTC

JUnit Testing : NullPointerException at ApplicationContext appContext = (ApplicationContext)FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

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:

 

java.lang.NullPointerException
    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?

I'd really appreciate your help!
-- 
View this message in context: http://old.nabble.com/JUnit-Testing-%3A-NullPointerException-at-ApplicationContext-appContext-%3D-------------------%28ApplicationContext%29FacesContextUtils.getWebApplicationContext%28FacesContext.getCurrentInstance%28%29%29--tp28696594p28696594.html
Sent from the My Faces - Dev mailing list archive at Nabble.com.