8 Replies Latest reply on Nov 28, 2007 8:52 AM by djfjboss

    Unit Testing With TestNG

      When I try to test my (simple) application using TestNG, variables that are automatically bijected when running outside of the test environment (such as instances of EntityManager, FacesMessages and Log) are no longer created automatically.

      EntityManager can be fixed via:

      EntityManagerFactory emf = Persistence.createEntityManagerFactory("proj3");
       emf.createEntityManager();
       EntityManager em = emf.createEntityManager();
       setField(action, "entityManager", em);


      Log can be fixed via:
      setField(action, "log", LogFactory.getLog(DummyTest.class));


      But trying to fix FacesMessages via:
      setField(action, "facesMessages", FacesMessages.instance());

      results in an IllegalStateException, stating that there is no active conversation context.

      What is the correct way to deal with such issues?

        • 1. Re: Unit Testing With TestNG
          pmuir

          Bijection should occur when you run integration tests (but obviously not when you run unit tests)

          • 2. Re: Unit Testing With TestNG

            So my question is: what do I need to do to fake this bijection when running in unit test mode?

            • 3. Re: Unit Testing With TestNG
              pmuir

              You'll have to set the properties yourself as you are doing.

              • 4. Re: Unit Testing With TestNG

                We seem to be talking at cross purposes here. I do understand that I need to set these properties myself - hence the references to setField in my initial post.

                However I do not understand why I get the exception when trying to set the FacesMessage variable. I looked at the source code but emerged none the wiser. I was therefore hoping someone on this forum could shed some light but at the moment we seem to be going round in circles. Is what I am doing basically wrong-headed or is there something else that might explain this exception?

                • 5. Re: Unit Testing With TestNG
                  pmuir

                  The correct way to deal with this is to do an integration test ;)

                  Its the FacesMessages.instance() that fails - you need to create a mock FacesMessages class if you *really* want to do this. This is because Seam has not booted at this point so none of the Seam built in components should be expected to work.

                  • 6. Re: Unit Testing With TestNG

                    This is the nub of the issue.

                    I was expecting SeamTest to provide mocks for the normal Seam environment and hence would provide a mocked FacesMessage but it sounds like that isn't the case - I certainly couldn't find one but I was wondering if I was missing something.

                    • 7. Re: Unit Testing With TestNG
                      pmuir

                      SeamTest is for integration testing not unit testing. A mock of FacesMessages isn't needed in an integration test. SeamTest mocks out JSF and Servlet, not Seam itself.

                      • 8. Re: Unit Testing With TestNG

                        Many thanks for confirming what I was beginning to suspect.

                        However, the reason I started looking at unit testing and TestNG was due to the unit testing chapter in the yuan/heute book which definitely does use SeamTest within a unit test, but is a little vague regarding the issues I encountered.