UI-Component Sets

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

Create Test using JUnit Runner

Create Test using JUnit Runner is no different than write other JUnit tests. Just use MyFacesTestRunner to run the tests.

@RunWith(MyFacesTestRunner.class)
@TestConfig(webappResourcePath = "webapp")
public class DemoTestCase
{
    @TestContainer
    private MyFacesContainer container;

    @Test
    public void testHelloWorld() throws Exception
    {
        container.startViewRequest("/helloWorld.xhtml");
        container.processLifecycleExecute();
        container.renderResponse();

        container.getClient().inputText("mainForm:name", "John Smith");
        // The button end current request and start a new request 
        // with a simulated submit 
        container.getClient().submit("mainForm:submit");

        container.processLifecycleExecute();

        Assert.assertEquals("/page2.xhtml", 
            container.getFacesContext().getViewRoot().getViewId());
        container.renderResponse();
        container.endRequest();
    }
}

The key object to control the lifecycle and the context in general is MyFacesContainer. With this object, it is possible to send a request, execute the lifecycle, make assertions and so on. Remember you are always on the server side, and the "client" is just something in the middle that mimic the steps done by the browser, so it is quite simple to write a custom client that manipulates MyFacesContainer for a third party library.

There are some annotations that allow to define methods that are called at some specified points:

  • @BeforeRequest and @AfterRequest
  • @SetupWebConfigParams : makes the method called before init but after servletContext is created
  • @BeforeJSFInit : called after servlet listeners are initialized but before JSF init.

Here are some examples about how to make it run with different environments:

Apache MyFaces Core + Apache OpenWebbeans

@RunWith(MyFacesTestRunner.class)
@TestConfig(
    scanAnnotations = false,
    webappResourcePath = "webapp",
    expressionFactory = "com.sun.el.ExpressionFactoryImpl")
@TestServletListeners({
    "org.apache.webbeans.servlet.WebBeansConfigurationListener"
})
public class MyFacesRunnerSimpleTestCase
{
    @TestContainer
    private MyFacesContainer container;

    @Inject
    @Named("helloWorld")
    private HelloWorldController helloWorldBean;

    @Test
    public void testHelloWorld() throws Exception
    {
        // ....

Apache MyFaces Core + Spring 3

Please note bean injection is only supported for CDI, but it is possible to enable it if you provide an implementation of InjectionProvider spi interface:

@RunWith(MyFacesTestRunner.class)
@TestConfig(
    scanAnnotations = true,
    oamAnnotationScanPackages = "jsf2jpa",
    webappResourcePath = "webapp",
    expressionFactory = "com.sun.el.ExpressionFactoryImpl")
@TestServletListeners({
    "org.springframework.web.context.ContextLoaderListener",
    "org.springframework.web.context.request.RequestContextListener"
})
public class MyFacesRunnerSimpleTestCase
{
    @TestContainer
    private MyFacesContainer container;

    @Test
    public void testHelloWorld() throws Exception
    {
        // ....

Apache MyFaces Core + Weld 1.1.x

@RunWith(MyFacesTestRunner.class)
@TestConfig(
    scanAnnotations = false,
    webappResourcePath = "webapp",
    expressionFactory = "com.sun.el.ExpressionFactoryImpl")
@TestServletListeners({
    "org.jboss.weld.environment.servlet.Listener"
})
public class MyFacesRunnerSimpleTestCase
{
    @TestContainer
    private MyFacesContainer container;

    @Inject
    @Named("helloWorld")
    private HelloWorldController helloWorldBean;

    @BeforeJSFInit
    public void init() throws NamingException
    {
        InitialContext ic = new InitialContext();
        ic.bind("java:comp/env/BeanManager", 
            container.getServletContext().getAttribute(
                "org.jboss.weld.environment.servlet.javax.enterprise.inject.spi.BeanManager"));
        container.getServletContext().setAttribute("javax.enterprise.inject.spi.BeanManager",
            container.getServletContext().getAttribute(
                "org.jboss.weld.environment.servlet.javax.enterprise.inject.spi.BeanManager"));
    }

    @Test
    public void testHelloWorld() throws Exception
    {
        // ....