For every JSF component on a page, you have the option of providing a component ID. If you don't provide one, JSF will create one for you. However, if you let JSF create the ID, you will have a hard time referencing the component in your tests. So at least use an ID for any component that you might want to test.
For Example:
<h:inputText value="#{foo.text}" id="input_foo_text"/>
<f:view>
<h:form id="form1">
<h:outputText value="Enter your name:" rendered="#{empty foo.text}" id="prompt"/>
<h:outputText value="Hello #{foo.text}" rendered="#{!empty foo.text}" id="greeting"/>
<h:inputText value="#{foo.text}" id="input_foo_text"/>
<h:message for="input_foo_text" styleClass="errorMessage"/>
<br/>
<h:commandButton value="Submit" action="/index.jsp" id="submit_button"/>
<h:commandButton value="Goodbye" action="/finalgreeting.jsp" id="goodbye_button"/>
</h:form>
</f:view>
public class JSFUnitTest extends org.apache.cactus.ServletTestCase
{
public static Test suite()
{
return new TestSuite( JSFUnitTest.class );
}
public void testInitialPage() throws IOException, SAXException
{
// Send an HTTP request for the initial page
JSFClientSession client = new JSFClientSession("/index.faces");
// A JSFServerSession gives you access to JSF state
JSFServerSession server = new JSFServerSession(client);
// Test navigation to initial viewID
assertEquals("/index.jsp", server.getCurrentViewID());
// Assert that the prompt component is in the component tree and rendered
UIComponent prompt = server.findComponent("greeting");
assertTrue(prompt.isRendered());
// Test a managed bean
assertEquals("Stan", server.getManagedBeanValue("#{foo.text}"));
}
}
<filter>
<filter-name>JSFUnitFilter</filter-name>
<filter-class>org.jboss.jsfunit.framework.JSFUnitFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>JSFUnitFilter</filter-name>
<servlet-name>ServletTestRunner</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>JSFUnitFilter</filter-name>
<servlet-name>ServletRedirector</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>ServletRedirector</servlet-name>
<servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
</servlet>
<servlet>
<servlet-name>ServletTestRunner</servlet-name>
<servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletRedirector</servlet-name>
<url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletTestRunner</servlet-name>
<url-pattern>/ServletTestRunner</url-pattern>
</servlet-mapping>
First, you will need the JSFUnit jar. Since JSFunit uses JUnit, Cactus, and HttpUnit for much of its infrastructue, you will need at least the minimum jars for these frameworks as well. Don't worry, you won't need to pollute your project with these dependencies. JSFUnit Documentation shows how to use Ant or Maven to only include these extra jars when you are testsing.
Here are the minimum jars you will need in your WEB-INF/lib.
http://localhost:8080/myjsfapp/ServletTestRunner?suite=com.foo.JSFUnitTest&xsl=cactus-report.xsl
Since the JSFUnit infrastructure is based on Apache Cactus, you can set up your tests to run in any way supported by Cactus. The only difference is that you use the JSFUnitFilter declaration as shown above.
We are currently deveoping cactify-like tools for Ant and Maven that will be used for JSFUnit. We have an Ant Task for the first JSFUnit beta and a Maven plugin should be ready soon.
To see live demos with more source code go to our demo page.