1 Reply Latest reply on Oct 12, 2007 4:25 PM by pmuir

    Log and EntityManager are not injected in integration test

    vanyatka

      Hi,

      I'm at the very beginning of adding integration testing to my Seam application, and I have the following problem. The EntityManager and Log objects used inside the action bean are nulls during invoking the tests, unless I set them manually (which AFAIK I should not).

      ( To clarify, the test just tries to access rootCategories context variable which is set in the SFSB, also shown below)

      @Test
      public void test() throws Exception {

      String cid = new FacesRequest("/addCategory.seam") {

      @Override
      protected void invokeApplication() throws Exception {
      getValue("#{rootCategories}");
      }
      }



      Here is the SFSB:

      @Local(CategoryManager.class)
      @Stateful
      @Name("categoryBean")
      public class CategoryBean {

      @Logger
      Log log;

      @In(create = true)
      private EntityManager em;

      @Out(required = false)
      private List<Category> rootCategories;

      public CategoryBean() {
      }

      @Factory("rootCategories")
      public List<Category> findRootCategories() {
      log.info("Refreshing the list of root Categories");
      Query query = em.createQuery("select c from Category c where c.parentCategory.id = 1 and c.id <> 1");
      List list = query.getResultList();
      for (Object o : list) {
      System.out.println("o = " + o);
      }
      return list;
      }

      @Remove
      @Destroy
      public void remove() {
      }

      }


      Now I run into NullPointerException inside findRootCategories() method, because neither em no log objects are intialized. Unit testing works fine, btw, when I add the following lines to the test body:


      EntityManagerFactory emf =
      Persistence.createEntityManagerFactory("MyCardsPersistenceUnit");
      EntityManager em = emf.createEntityManager();
      setField(categoryBean, "em", em);
      setField(categoryBean, "log", Logging.getLog(CategoryBean.class));


      Can anyone give a hint?
      Thanks,