Thanks Matthias. Since I sent this message, I dug into MyFaces test tree and found you were using AbstractJsfTestCase. So I started to play with it and got everything passing. Now I need to create a version that subclasses Spring's AbstractTransactionalDataSourceSpringContextTests so I can get automatic rollback in my tests. Mocks would work with the current one, but I want to have the ability to do integration tests as well.
Cheers, Matt Matthias Wessendorf-4 wrote: > > see > > http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/FacesTestCase.java?revision=555932&view=markup > > or, using Shale Tiger (MockFacesContext12): > > http://svn.apache.org/viewvc/myfaces/core/branches/1_2_1/impl/src/test/java/org/apache/myfaces/application/ApplicationImplTest.java?view=markup > > -M > > > > On 8/27/07, mraible <[EMAIL PROTECTED]> wrote: >> >> Werner - do you have an example of initializing a FacesContext in a JUnit >> Test? >> >> Thanks, >> >> Matt >> >> >> Werner Punz-2 wrote: >> > >> > Ok Matt I ran into a similar problem in a testmigration of an old >> > project to myfaces 1.2. >> > >> > Following, make sure that you are on a current container version >> > implementing the newest jsp and servlet apis (in my case tomcat running >> > on java 5) >> > then remove all traces to the jsp-api and any el-implementation >> (however >> > commons-el has to be still there if you use tomahawk otherwise you will >> > get an error, tomahawk references commons-el logger somewhere) >> > >> > if done it should work. >> > >> > The main problem with the migration is that old installs relied on a >> > hogde podge of various deps into el implementations and servlet specs, >> > 1.2 is pure jee5 so any older version of the servlet spec or el or jsp >> > in your runtime classpath is a source of possible problems. >> > >> > >> > >> > >> > mraible schrieb: >> >> 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: >> >> >> >> ERROR - DefaultFacesInitializer.initFaces(126) | Error initializing >> >> MyFaces: >> >> null >> >> java.lang.NullPointerException >> >> at >> >> >> org.apache.myfaces.webapp.DefaultFacesInitializer.initFaces(DefaultFacesInitializer.java:102) >> >> at >> >> >> org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:57) >> >> at >> >> >> org.appfuse.web.BasePageTestCase.performFacesContextConfig(BasePageTestCase.java:58) >> >> at >> >> org.appfuse.web.BasePageTestCase.<clinit>(BasePageTestCase.java:50) >> >> at >> sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native >> >> Method) >> >> at >> >> >> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) >> >> at >> >> >> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.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(NativeConstructorAccessorImpl.java:39) >> >> at >> >> >> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.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.<init>(TestSuite.java:140) >> >> at >> sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native >> >> Method) >> >> at >> >> >> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) >> >> at >> >> >> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) >> >> at >> >> java.lang.reflect.Constructor.newInstance(Constructor.java:494) >> >> at >> >> >> org.apache.maven.surefire.junit.JUnitTestSet.constructTestObject(JUnitTestSet.java:151) >> >> at >> >> >> org.apache.maven.surefire.junit.JUnitTestSet.getTestCount(JUnitTestSet.java:247) >> >> at >> >> >> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.locateTestSets(AbstractDirectoryTestSuite.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.java:25) >> >> at java.lang.reflect.Method.invoke(Method.java:585) >> >> at >> >> >> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.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; >> >> <listener> >> >> >> >> >> <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> >> >> </listener> >> >> >> >> at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:90) >> >> at >> >> >> org.appfuse.web.BasePageTestCase.performFacesContextConfig(BasePageTestCase.java:60) >> >> at >> >> org.appfuse.web.BasePageTestCase.<clinit>(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.<init>(TestSuite.java:140) >> >> >> >> Lines 55-61 are: >> >> <snip> >> >> StartupServletContextListener facesListener = >> >> new StartupServletContextListener(); >> >> ServletContextEvent event = new >> >> ServletContextEvent(servletContext); >> >> facesListener.contextInitialized(event); >> >> >> >> LifecycleFactory lifecycleFactory = >> >> (LifecycleFactory) >> >> FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); >> >> </snip> >> >> >> >> 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#a12353938 >> Sent from the MyFaces - Users mailing list archive at Nabble.com. >> >> > > > -- > Matthias Wessendorf > > further stuff: > blog: http://matthiaswessendorf.wordpress.com/ > mail: matzew-at-apache-dot-org > > -- View this message in context: http://www.nabble.com/Testing-JSF-1.2-Managed-Beans-vs.-JSF-1.1-tf4103841.html#a12355334 Sent from the MyFaces - Users mailing list archive at Nabble.com.