2 Replies Latest reply on Aug 19, 2009 7:52 AM by guima

    Right way to write JSFUnit tests

    guima

      I'm testing JSFUnit with Maven. All articles published in wiki were very useful, specially "JSFUnitWithMaven". It works great.

      So, i tried to implement PageMessages.java like in http://wiki.apache.org/myfaces/Displaying_Errors/Infos/Warnings_in_JSF_Pages and wrote some tests like

      public class PageMessagesTest extends ServletTestCase {
      private JSFClientSession client;
      private JSFServerSession server;

      public void setUp() throws IOException, SAXException {
      JSFSession jsfSession = new JSFSession("/index.faces");
      this.client = jsfSession.getJSFClientSession();
      this.server = jsfSession.getJSFServerSession();
      }

      public static Test suite() {
      return new TestSuite(PageMessagesTest.class);
      }

      public void testGetName() throws IOException, SAXException {
      FacesContext context = server.getFacesContext();
      context.addMessage("messages", new FacesMessage(
      FacesMessage.SEVERITY_INFO, "PageMessages test...", null));
      assertEquals("PageMessages", (String)server.getManagedBeanValue("#{pageMessages.beanName}"));
      }

      }

      My PageMessagesTest.java is in JSFUnit submodule.

      I received the following error message
      "Test failed, Exception message = [null expected: but was:]"

      Does it make sense the way i want to write some unit tests like that? Is it better to write a Cactus test in my web submodule? Should i use JSFUnit just to test existing ManagedBean call in my pages?

      Thanks,
      Eduardo

        • 1. Re: Right way to write JSFUnit tests
          ssilvert

          Hi Eduardo,

          It's probably not appropriate to add messages to the FacesContext at that point. Inside the test, you are looking at the FacesContext as it exists at the end of the request. Since the request is over, and the JSF lifecycle is over, a new FacesMessage added to the FacesContext is in an undefined state.

          You could install a PhaseListener for test purposes that adds a FacesMessage at the desired time during the JSF lifecycle. Then do assertions against the message in your test.

          I've considered adding a feature that actually gives your test a callback during the lifecycle for just this kind of thing. But until your post, I haven't seen any demand for such a feature.

          The usual way people test FacesMessages today is to just let the test go through the user interface and let the FacesMessage get created naturally. Then you do assertions to make sure that the FacesMessage was added as expected.

          Stan

          • 2. Re: Right way to write JSFUnit tests
            guima

            Hi Stan,

            Thank you for your response. You told me exactly what was happening and explained some alternatives. It made me see the importance of a good understanding of JSF lifecycle.

            I wrote tests just letting FacesMessages get created naturally. It was very easy and quick to create tests getting FacesMessages and doing the assertions. JSFUnit is excellent.

            Eduardo