myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mraible <m...@raibledesigns.com>
Subject Testing JSF 1.2 Managed Beans vs. JSF 1.1
Date Wed, 18 Jul 2007 15:33:16 GMT

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#a11670577
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Mime
View raw message