UI-Component Sets

Home » Wiki » MyFaces Core » MyFaces Core User Guide » JSF and MyFaces Howtos » Unit Testing

Setup Test Environment with Maven

First of all, you need to include the required test dependencies into your project:

<!-- MyFaces 2.2.x should be available as a compile dependency or
     as a test dependency -->
<!-- Base library to run MyFaces in a JUnit test. If you are
     using MyFaces Core 2.2.1 use the same version number -->
<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-impl-test</artifactId>
    <version>2.2.1</version>
    <scope>test</scope>
</dependency>

<!-- The library uses Myfaces-Test mock objects -->
<dependency>
    <groupId>org.apache.myfaces.test</groupId>
    <artifactId>myfaces-test22</artifactId>
    <version>1.0.6</version>
    <scope>test</scope>
</dependency>

<!-- Don't forget junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>

<!-- Don't forget to include an EL implementation, otherwise
     you will use the mock EL implementation bundled with
     MyFaces Test -->
<dependency>
    <groupId>org.eclipse.jetty.orbit</groupId>
    <artifactId>javax.el</artifactId>
    <version>2.2.0.v201108011116</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.orbit</groupId>
    <artifactId>com.sun.el</artifactId>
    <version>2.2.0.v201108011116</version>
    <scope>test</scope>
</dependency>

Suppose a typical web application using maven. It usually has the following structure:

src
 |- main
      |- java
      |- resources
      |- webapp
 |- test
      |- java
      |- resources

In the test phase, by default the environment only has access to the resources in src/main/java, src/main/resources, src/test/java and src/test/resources through the classpath. So one thing you need is make the webapp folder accesible as test resources through the classpath with something like this.

<build>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
        <testResource>
            <directory>${project.basedir}/src/main/webapp</directory>
            <targetPath>webapp</targetPath>
        </testResource>
    </testResources>

    <!-- ... -->
</build>