This guide will get you started using JSFUnit 2.0.0.Beta2 using Arquillian and JUnit 4.  The classic style using Cactus and JUnit 3 is still supported but not documented here.

View or run a working example.

Click here for instructions to download and run a full working example.

First, always follow the JSFUnit Golden Rule: Use Component ID's

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"/>
	

Create a JSP or Facelets page

	

<f:view>
   <h:form id="form1">
     <h:outputText value="Enter Your Name:"/>
     <h:inputText id="name"/>
     <h:commandButton id="submit_button"/>
   </h:form>
   <h:outputText id="greeting"
     rendered="#{not empty request.getParameter('form1:name')}"
     value=" Hello #{request.getParameter('form1:name')}"/>
</f:view>


Create an Arquillian test class

See the Arquillian Documentation for details.

Note that we add two beans.xml files to our deployment.  These are used for CDI support.  They are required if you want to use JSFUnit Annotations like @InitialPage that help you initialize your JSFSession.

	

@RunWith(Arquillian.class)
public class HelloJSFTest {
   @Deployment
   public static WebArchive createDeployment() {
      return  ShrinkWrap.create(WebArchive.class, "test.war")
            .setWebXML(new File("src/main/webapp/WEB-INF/web.xml"))
            .addPackage(Package.getPackage("org.jboss.jsfunit.example.hellojsf")) // my test package
            .addResource(new File("src/main/webapp", "index.xhtml"))
            .addWebResource(new File("src/main/webapp/WEB-INF/faces-config.xml"), "faces-config.xml");

   }

}


Create a JUnit test method using the JSFUnit API

The full JSFUnit javadoc is here.  The javadoc for JSFUnit 2.0 is unchanged from 1.3.  However, you can now use annotations to simplify your code.  The annotaiton reference is here.

 

	

@Test @InitialPage("/index.faces")
public void testInitialPage(JSFServerSession server, JSFClientSession client) throws IOException {
    // Test navigation to initial viewID
    Assert.assertEquals("/index.xhtml", server.getCurrentViewID());

    // Set the param and submit
    client.setValue("name", "Stan");
    client.click("submit_button");

    // Assert that the greeting component is in the component tree and rendered
    UIComponent greeting = server.findComponent("greeting");
    Assert.assertTrue(greeting.isRendered());

    // Test a managed bean using EL. We cheat and use the request object.
    Assert.assertEquals("Stan", server.getManagedBeanValue("#{request.getParameter('form1:name')}"));
}


Prepare your Maven pom.xml

Your pom.xml should contain references to the repositories and dependencies below.  Other dependencies will depend on your choice of container as shown in the Aqruillian documentation.

 

	

<repositories>
        <repository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Maven Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/groups/public</url>
            <layout>default</layout>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
          <id>jboss-public-repository-group</id>
          <name>JBoss Public Repository Group</name>
          <url>http://repository.jboss.org/nexus/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <properties>
        <version.jsfunit>2.0.0.Beta2</version.jsfunit>
        <version.arquillian>1.0.0.CR4</version.arquillian>
        <version.arquillian.jboss>1.0.0.CR2</version.arquillian.jboss>
        <version.shrinkwrap>1.0.0-beta-5</version.shrinkwrap>
        <version.shrinkwrap.descriptors>1.1.0-alpha-2</version.shrinkwrap.descriptors>
        <version.jboss_60>6.0.0.Final</version.jboss_60>
    </properties>

  <!-- Dependencies common to all containers -->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <version>${version.arquillian}</version>
            <scope>test</scope>
        </dependency>
   
        <dependency>
            <groupId>org.jboss.arquillian.protocol</groupId>
            <artifactId>arquillian-protocol-servlet</artifactId>
            <version>${version.arquillian}</version>
            <scope>test</scope>
        </dependency>
   
        <dependency>
            <groupId>org.jboss.arquillian.container</groupId>
            <artifactId>arquillian-container-spi</artifactId>
            <version>${version.arquillian}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.faces</groupId>
            <artifactId>jboss-jsf-api_2.1_spec</artifactId>
            <version>2.0.0.Beta1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_3.0_spec</artifactId>
            <version>1.0.0.Final</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.jsfunit</groupId>
            <artifactId>jsfunit-arquillian</artifactId>
            <version>${version.jsfunit}</version>
            <scope>test</scope>
        </dependency>
       
        <dependency>
            <groupId>org.jboss.jsfunit</groupId>
            <artifactId>jboss-jsfunit-core</artifactId>
            <version>${version.jsfunit}</version>
            <scope>test</scope>
        </dependency>
       
        <dependency>
            <groupId>org.jboss.shrinkwrap.descriptors</groupId>
            <artifactId>shrinkwrap-descriptors-impl</artifactId>
            <version>${version.shrinkwrap.descriptors}</version>
            <scope>test</scope>
        </dependency>
   
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-api-maven</artifactId>
            <version>${version.shrinkwrap}</version>
            <scope>test</scope>
        </dependency>
   
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
            <version>${version.shrinkwrap}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

  <profiles>
    <profile>
      <id>jbossas-remote-6</id>
      <dependencies>
      <dependency>
          <groupId>org.jboss.arquillian.container</groupId>
          <artifactId>arquillian-jbossas-remote-6</artifactId>
          <version>${version.arquillian.jboss}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.jboss.jbossas</groupId>
          <artifactId>jboss-as-profileservice-client</artifactId>
          <version>${version.jboss_60}</version>
          <type>pom</type>
        </dependency>
      </dependencies>
    </profile>

    <profile>
      <id>jbossas-remote-7</id>
      <dependencies>
        <dependency>
          <groupId>org.jboss.as</groupId>
          <artifactId>jboss-as-arquillian-container-remote</artifactId>
          <version>7.0.1.Final</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </profile>

  </profiles>


Run the tests.

For JBoss AS6: mvn -Pjbossas-remote-6 test

For JBoss AS7: mvn -Pjbossas-remote-7 test