>From: mraible <[EMAIL PROTECTED]> 
>
> 
> I had the following BasePageTestCase class that I've been using to test my 
> JSF 1.1 pages. Unfortunately, after upgrading to JSF 1.2, it no longer 
> works. Here's the stack trace: 
>

Sorta looks like there is not a mock implementation for the JspFactory.  Just 
poking thru the code,
it looks like it's trying to find a shared EL context.  You might give 
shale-test a try.


Gary
 
> ERROR - DefaultFacesInitializer.initFaces(126) | Error initializing MyFaces: 
> null 
> java.lang.NullPointerException 
> at 
> org.apache.myfaces.webapp.DefaultFacesInitializer.initFaces(DefaultFacesInitiali
>  
> zer.java:102) 
> at 
> org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(Start
>  
> upServletContextListener.java:57) 
> at 
> org.appfuse.web.BasePageTestCase.performFacesContextConfig(BasePageTestCase.java
>  
> :58) 
> at 
> org.appfuse.web.BasePageTestCase.(BasePageTestCase.java:50) 
> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 
> Method) 
> at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorI
>  
> mpl.java:39) 
> at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorA
>  
> ccessorImpl.java:27) 
> org.apache.maven.surefire.booter.SurefireExecutionException: 
> org.appfuse.web.UserFormTest; nested exception is 
> java.lang.ExceptionInInitializerError: null; nested exception is 
> org.apache.maven.surefire.testset.TestSetFailedException: 
> org.appfuse.web.UserFormTest; nested exception is 
> java.lang.ExceptionInInitializerError: null 
> org.apache.maven.surefire.testset.TestSetFailedException: 
> org.appfuse.web.UserFormTest; nested exception is 
> java.lang.ExceptionInInitializerError: null 
> java.lang.ExceptionInInitializerError 
> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 
> Method) 
> at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorI
>  
> mpl.java:39) 
> at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorA
>  
> ccessorImpl.java:27) 
> at java.lang.reflect.Constructor.newInstance(Constructor.java:494) 
> at junit.framework.TestSuite.createTest(TestSuite.java:54) 
> at junit.framework.TestSuite.addTestMethod(TestSuite.java:280) 
> at junit.framework.TestSuite.(TestSuite.java:140) 
> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 
> Method) 
> at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorI
>  
> mpl.java:39) 
> at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorA
>  
> ccessorImpl.java:27) 
> at java.lang.reflect.Constructor.newInstance(Constructor.java:494) 
> at 
> org.apache.maven.surefire.junit.JUnitTestSet.constructTestObject(JUnitTestSet.ja
>  
> va:151) 
> at 
> org.apache.maven.surefire.junit.JUnitTestSet.getTestCount(JUnitTestSet.java:247)
>  
> at 
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.locateTestSets(Abstra
>  
> ctDirectoryTestSuite.java:104) 
> at 
> org.apache.maven.surefire.Surefire.createSuiteFromDefinition(Surefire.java:150)
>  
> at org.apache.maven.surefire.Surefire.run(Surefire.java:111) 
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
> at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
> at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
>  
> a:25) 
> at java.lang.reflect.Method.invoke(Method.java:585) 
> at 
> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBoote
>  
> r.java:290) 
> at 
> org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:818) 
> Caused by: java.lang.IllegalStateException: No Factories configured for this 
> Application. This happens if the faces-initialization does not work at all - 
> make sure that you properly include all configuration settings necessary for 
> a basic faces application and that all the necessary libs are included. Also 
> check the logging output of your web application and your container for any 
> exceptions! 
> If you did that and find nothing, the mistake might be due to the fact that 
> you use some special web-containers which do not support registering 
> context-listeners via TLD files and a context listener is not setup in your 
> web.xml. 
> A typical config looks like this; 
> 
> 
> org.apache.myfaces.webapp.StartupServletContextListener> r-class> 
> 
> 
> at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:90) 
> at 
> org.appfuse.web.BasePageTestCase.performFacesContextConfig(BasePageTestCase.java
>  
> :60) 
> at 
> org.appfuse.web.BasePageTestCase.(BasePageTestCase.java:50) 
> ... 22 more 
> at java.lang.reflect.Constructor.newInstance(Constructor.java:494) 
> at junit.framework.TestSuite.createTest(TestSuite.java:54) 
> at junit.framework.TestSuite.addTestMethod(TestSuite.java:280) 
> at junit.framework.TestSuite.(TestSuite.java:140) 
> 
> Lines 55-61 are: 
> 
> StartupServletContextListener facesListener = 
> new StartupServletContextListener(); 
> ServletContextEvent event = new ServletContextEvent(servletContext); 
> facesListener.contextInitialized(event); 
> 
> LifecycleFactory lifecycleFactory = 
> (LifecycleFactory) 
> FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 
> 
> 
> Here's the full class: 
> 
> BasePageTestCase.java: 
> 
> package org.appfuse.web; 
> 
> import javax.faces.FactoryFinder; 
> import javax.faces.application.Application; 
> import javax.faces.application.ApplicationFactory; 
> import javax.faces.context.FacesContext; 
> import javax.faces.context.FacesContextFactory; 
> import javax.faces.el.ValueBinding; 
> import javax.faces.lifecycle.Lifecycle; 
> import javax.faces.lifecycle.LifecycleFactory; 
> import javax.faces.webapp.FacesServlet; 
> import javax.servlet.ServletContextEvent; 
> import javax.servlet.ServletContextListener; 
> 
> import junit.framework.TestCase; 
> import org.apache.myfaces.webapp.StartupServletContextListener; 
> 
> import org.apache.commons.logging.Log; 
> import org.apache.commons.logging.LogFactory; 
> import org.springframework.mock.web.MockHttpServletRequest; 
> import org.springframework.mock.web.MockHttpServletResponse; 
> import org.springframework.mock.web.MockServletConfig; 
> import org.springframework.mock.web.MockServletContext; 
> import org.springframework.web.context.ContextLoader; 
> import org.springframework.web.context.ContextLoaderListener; 
> import org.springframework.web.context.WebApplicationContext; 
> import org.springframework.web.jsf.FacesContextUtils; 
> 
> public abstract class BasePageTestCase extends TestCase { 
> protected final Log log = LogFactory.getLog(getClass()); 
> protected static FacesContext facesContext; 
> protected static MockServletConfig config; 
> protected static MockServletContext servletContext; 
> protected static WebApplicationContext ctx; 
> 
> // This static block ensures that Spring's BeanFactory and JSF's 
> // FacesContext is only loaded once for all tests. 
> static { 
> servletContext = new MockServletContext(""); 
> // This static block ensures that Spring's BeanFactory and JSF's 
> // FacesContext is only loaded once for all tests. 
> servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, 
> "/WEB-INF/applicationContext*.xml"); 
> 
> ServletContextListener contextListener = new 
> ContextLoaderListener(); 
> ServletContextEvent event = new ServletContextEvent(servletContext); 
> contextListener.contextInitialized(event); 
> 
> config = new MockServletConfig(servletContext); 
> facesContext = performFacesContextConfig(); 
> ctx = 
> FacesContextUtils.getRequiredWebApplicationContext(facesContext); 
> } 
> 
> protected static FacesContext performFacesContextConfig() { 
> StartupServletContextListener facesListener = 
> new StartupServletContextListener(); 
> ServletContextEvent event = new ServletContextEvent(servletContext); 
> facesListener.contextInitialized(event); 
> 
> LifecycleFactory lifecycleFactory = 
> (LifecycleFactory) 
> FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 
> 
> Lifecycle lifecycle = 
> lifecycleFactory.getLifecycle(getLifecycleId()); 
> 
> FacesContextFactory facesCtxFactory = 
> (FacesContextFactory) 
> FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); 
> 
> FacesContext ctx = 
> facesCtxFactory.getFacesContext(servletContext, 
> new MockHttpServletRequest(), 
> new MockHttpServletResponse(), 
> lifecycle); 
> 
> return ctx; 
> } 
> 
> protected static String getLifecycleId() { 
> String lifecycleId = 
> servletContext.getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR); 
> 
> return (lifecycleId != null) ? lifecycleId 
> : LifecycleFactory.DEFAULT_LIFECYCLE; 
> } 
> 
> /** 
> * Get managed bean based on the bean name. 
> * 
> * @param beanName the bean name 
> * @return the managed bean associated with the bean name 
> */ 
> protected Object getManagedBean(String beanName) { 
> return getValueBinding(getJsfEl(beanName)).getValue(facesContext); 
> } 
> 
> private Application getApplication() { 
> ApplicationFactory appFactory = 
> (ApplicationFactory) 
> FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); 
> 
> return appFactory.getApplication(); 
> } 
> 
> private ValueBinding getValueBinding(String el) { 
> return getApplication().createValueBinding(el); 
> } 
> 
> private String getJsfEl(String value) { 
> return "#{" + value + "}"; 
> } 
> } 
> 
> Thanks, 
> 
> Matt 
> 
> 
> -- 
> View this message in context: 
> http://www.nabble.com/Testing-JSF-1.2-Managed-Beans-vs.-JSF-1.1-tf4103841.html#a
>  
> 11670577 
> Sent from the MyFaces - Users mailing list archive at Nabble.com. 
> 

Reply via email to