MyFaces Test provide some base test classes for JUnit 3 and 4. It is recommended to use JUnit 4.

The most common used class is AbstractJsfTestCase. This base class initialize a simulated environment for you, so the first step is create a test clase extending from it:

public class SimpleJSFTest extends AbstractJsfTestCase
{

   @Test
   public void testSomethingSimple() throws Exception
   {

   }

}

That's it. Inside that you can call FacesContext.getCurrentInstance() and a functional mock FacesContext will be returned, or you can just use facesContext variable to grab it. There are other variables like request, response, session, so you can add beans into request, session or application scope.

If this simple environment is not enough for you, you can override protected methods like setUp() or setUpServletObjects() and others, to customize it to your needs. If you need to mix real JSF artifacts with mock instances you can do it with AbstractJsfConfigurableMockTestCase. For example take a look at FaceletTestCase. It extends from AbstractJsfConfigurableMockTestCase, and uses a mixture between mock objects and MyFaces Core object to allow build facelet views and test it.

Check into myfaces source code for examples about how to use it.

  • No labels