|
JBoss JSFUnit is the first open source project dedicated to testing JSF applications.You may wonder why we need another web testing tool: the answer is that JSFunit is simply different from other tools.
The big difference between JSFUnit and other stuff like like WebTest, HttpUnit, HTMLUnit is that JSFUnit includes both testing the client side and the server side.
For example, besides from testing client navigation, JSFUnit can do things like
evaluating JSFBeans using EL Expressions. Moreover JSF Unit leverages the JSF Api
by providing any kind of information related to the state of the JSF application.
Setting up the environment specifically for JBoss:
Here are the jars you will need in your WEB-INF/lib:
Notice if you are using JSFUnit outside of JBoss you need to add also the following:
Additionally you need to place in your WEB root the cactus report schema:
http://jakarta.apache.org/cactus/misc/cactus-report.xsl
Configure web.xml to use JSF and JSFUnit:
The following web.xml includes references to JSF Faces Config Servlet and JSFUnit Filters:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>com.sun.faces.enableRestoreView11Compatibility</param-name>
<param-value>true</param-value>
</context-param>
<display-name>JsfUnitTest</display-name>
<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>Faces Config</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Config</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletRedirector</servlet-name>
<servlet-class>org.jboss.jsfunit.framework.JSFUnitServletRedirector</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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
Add a simple JSP page
The following JSP page basically performs an user login against a JSFManagedBean :
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> [1]
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<body>
<f:view>
<h:form id="form1">
<h:outputText value="Enter your name:" id="prompt"/>
<h:inputText value="#{bean.username}" id="username"/>
<h:inputText value="#{bean.password}" id="password"/>
<br/>
<h:commandButton value="Submit" action="#{bean.login}" id="login"/>
</h:form>
</f:view>
</body>
</html>
This is the corresponding Bean with username and password properties and the login() method:
package sample;
public class MyBean {
String username="guest";
String password="guest";
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
if (username != null && username.equals("john") &&
password != null && password.equals("ripple")) {
return "success";
}
return "failure";
}
}
And finally this is the faces-config.xml configuration file:
<?xml version="1.0"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<managed-bean>
<managed-bean-name>bean</managed-bean-name>
<managed-bean-class>sample.MyBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
Add a Test Case:
Since your project includes Cactus test case suite, you can write a simple ServletTestCase as a java class, which will be added into WEB-INF/classes of your web application.
This Test Case includes both server testing and client testing and is rather easy to understand. It evaluates the initial value of the username property (set to guest) and then simulates the login using the credentials "john" "ripple".
If login is succesful, the last assertion will be true.
package com.sample;
import java.io.IOException;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
import org.jboss.jsfunit.jsfsession.JSFSession;
import junit.framework.Test;
import junit.framework.TestSuite;
public class JSFUnitTest extends org.apache.cactus.ServletTestCase
{
public static Test suite()
{
return new TestSuite( JSFUnitTest.class );
}
public void testInitialPage() throws IOException
{
// Send an HTTP request for the initial page
JSFSession jsfSession = new JSFSession("/index.jsf");
// A JSFClientSession emulates the browser and lets you test HTML
JSFClientSession client = jsfSession.getJSFClientSession();
// A JSFServerSession gives you access to JSF state
JSFServerSession server = jsfSession.getJSFServerSession();
// Test navigation to initial viewID
assertEquals("/index.jsp", server.getCurrentViewID());
// Assert that the current value of username is guest
assertEquals("guest", server.getManagedBeanValue("#{bean.name}"));
// set values and hit login
client.setValue("form1:username", "john");
client.setValue("form1:password", "ripple");
client.click("login");
// Test navigation
assertEquals("/welcome.jsp", server.getCurrentViewID());
}
}
In order to execute the JSFUnit test you have to invoke the ServletTestRunner passing as argument the UnitTest name and the xsl stylesheet. Supposing that your web context is "JsfUnitTest":
http://localhost:8080/JsfUnitTest/ServletTestRunner?suite=com.sample.JSFUnitTest&xsl=cactus-report.xsl

References:
http://labs.jboss.com/jsfunit/gettingstarted.html
|