Advertise with us

Our eBooks

JSFUnit tutorial

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 testing both 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:

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="" xmlns:xsi=""




        <servlet-name>Faces Config</servlet-name>
        <servlet-name>Faces Config</servlet-name>






Add a simple JSP page

The following JSP page basically performs an user login against a JSFManagedBean :

<%@ taglib uri="" prefix="h"%> [1]
<%@ taglib uri="" prefix="f"%>

   <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"/>
   <h:commandButton value="Submit" action="#{bean.login}" id="login"/>

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=""
 xmlns:xsi="" xsi:schemaLocation="">


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 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("#{}"));  
      // set values and hit login
      client.setValue("form1:username", "john");
      client.setValue("form1:password", "ripple");"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":
jsfunit tutorial


© 2021 All Rights Reserved.

Please publish modules in offcanvas position.