Last updated July 29, 2009 15:35, by Jason Lee
=FacesTester=
''Try it out. Give us feedback. Make it (and your app) better 8-)
FacesTester is a JSF unit testing framework which allows you to exercise your JavaServer Faces application (managed beans, UIComponents, PhaseListeners, etc) outside a container. This allows for faster testing, as well simplifying testing in a continuous integration environment.
__TOC__
=== Dependencies ===
The dependencies for FacesTester are little more than what your application already has. Specifically, here are the classes that you likely will not already have in your project:
* Spring Mock 2.0.8 (dependency removed in version 0.3.0)
* Java Persistence API 1.0 (optional)
=== Usage ===
Using FacesTester is pretty straightforward. This example is taken from one of FacesTester's own integration tests:
<textarea name="code" class="xml"><nowiki>
public class WhenNavigatingToPage {
private static FacesTester tester;
@BeforeClass
public static void setUp() throws Exception {
tester = new FacesTester();
}
@Test
public void shouldBeAbleToAssertValueOfComponents() throws Exception {
assertThat(tester.requestPage("/address.xhtml").getComponentWithId("form:stateLabel").getValueAsString(),
is("State"));
}
@Test(expected = AssertionError.class)
public void shouldBeAbleToAssertValueOfNoExistentComponents() {
tester.requestPage("/address.xhtml").getComponentWithId("unknown");
}
@Test
public void shouldBeAbleToEvaluateEl() throws Exception {
assertThat(tester.requestPage("/address.xhtml").getComponentWithId("form:elTest").getValueAsString(), is("9"));
}
@Test
public void shouldBeAbleToTestRendered() throws Exception {
FacesComponent component = tester.requestPage("/address.xhtml").getComponentWithId("form:renderedTest");
assertThat(component.getValueAsString(), is("RenderedTest"));
assertEquals(component.isRendered(), false);
}
}
</nowiki></textarea>
The basic Object is <code>FacesTester</code>. Using that object, we can request pages, retrieve specific components, etc. FacesTester configures itself based on the <code>web.xml</code> in your project, as well as any <code>faces-config.xml</code> files it may find. The web descriptor is located using a simple searching algorithm in several possible locations for a Maven-based project. If you are not using Maven, or the file is an unusual location, you can specify the location of the descriptor file by using the <code>facestester.webAppPath</code> system property.
=== Injection ===
One of the hard parts about testing JSF applications is the heavy use of injection. FacesTester provides a facility for performing injection implicitly, using pre-registered, arbitrary objects. For example, for this managed bean:
<textarea name="code" class="xml"><nowiki>
public class ManagedBeanWithJpa {
@PersistenceContext(unitName = "em", type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager1;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager1 = entityManager;
}
}
</nowiki></textarea>
the unit test might look like this:
<textarea name="code" class="xml"><nowiki>
InjectionManager.registerObject("em", new MockEntityManager());
ManagedBeanWithJpa mb = tester.getManagedBean(ManagedBeanWithJpa.class, "jpaBean");
assertNotNull(mb.getEntityManager());
</nowiki></textarea>
=== Obtaining FacesTester ===
For Maven users, the following dependency in your pom.xml will be sufficient:
<textarea name="code" class="xml"><nowiki>
<dependency>
<groupId>com.steeplesoft.jsf</groupId>
<artifactId>facestester</artifactId>
<version>0.2</version>
<scope>test</scope>
</dependency>
</nowiki></textarea>
For non-Maven users (and non-Ivy users), the jar file can be downloaded from the java.net [http://download.java.net/maven/2/com/steeplesoft/jsf/facestester/ Maven repository], or from the [http://kenai.com/projects/facestester/downloads download section] here on the project site.
=== JavaDoc ===
The JavaDocs for the current release can be found at [http://steeplesoft.com/facestester/api/ steeplesoft.com].





