1 Reply Latest reply on May 24, 2007 8:45 AM by Gavin King

    Seam - Embedded Ejb - Logger

    philippe mercier Newbie

      Hi everybody,

      I use embedded-ejb with testng (tests are launch via maven2 - surefire).
      All work fine !

      Now i would like to use @Logger in my session bean as this :

      @Stateless
      public class DossierManagerImpl implements DossierManagerLocal {
      
       @Logger
       private Log logger;
      
       @PersistenceContext
       private EntityManager em;
      
       /**
       * Constructeur vide
       */
       public DossierManagerImpl() {
       }
      
       /**
       * Constructeur pour les tests
       * @param em
       */
       public DossierManagerImpl(EntityManager em) {
       this.em = em;
       }
      
       /*
       * (non-Javadoc)
       * @see be.ucm.ss.comptaclient.c397.session.DossierManagerLocal#insert(be.ucm.ss.comptaclient.c397.entity.Dossier)
       */
       public void insert(Dossier ccDossier) {
       logger.debug("insert dossier : nune = " + ccDossier.getNune());
       em.persist(ccDossier);
       }
      
       /*
       * (non-Javadoc)
       * @see be.ucm.ss.comptaclient.c397.session.DossierManagerLocal#update(be.ucm.ss.comptaclient.c397.entity.Dossier)
       */
       public void update(Dossier ccDossier) {
       em.merge(ccDossier);
       }
      
       /*
       * (non-Javadoc)
       * @see be.ucm.ss.comptaclient.c397.session.DossierManagerLocal#findAll()
       */
       @SuppressWarnings("unchecked")
       public List<Dossier> findAll() {
       Query q = (Query) em.createQuery("from Dossier");
       return q.getResultList();
       }
      
       /*
       * (non-Javadoc)
       * @see be.ucm.ss.comptaclient.c397.session.DossierManagerLocal#findFromNune(java.lang.String)
       */
       public Dossier findFromNune(String nune) {
       System.out.println(nune);
       Dossier dossier = em.find(Dossier.class, nune);
       return dossier;
       }
      
      }
      



      but when my test class call the "insert" method of the session bean, i have an NullPointerException at the line that reference the logger.

      here is my Test class :
      package be.ucm.ss.comptaclient.c397.session;
      
      import java.util.List;
      
      import javax.persistence.EntityManager;
      
      import org.testng.annotations.Test;
      
      import be.ucm.ss.comptaclient.c397.entity.Dossier;
      import be.ucm.util.EmbeddedEjbHandler;
      
      @Test
      public class DossierManagerImplTest {
       public void testInsert() {
       EntityManager em = EmbeddedEjbHandler.getInstance().getEntityManager();
       em.getTransaction().begin();
      
       Dossier d = new Dossier();
       d.setNune("1234567890");
       d.setNumonss("123456789");
       d.setDossiermaitre("12345");
       d.setNumsuccu("123");
       d.setGestionnairecompta("PHILOU");
       d.setAdresseonss1("adr1");
       d.setAdresseonss2("adr2");
       d.setAdresseonss3("adr3");
       d.setAdresseonss4("adr4");
       d.setAdresseonss4("adr5");
       d.setAdresseonss4("adr6");
       d.setAdresseonss4("adr7");
       d.setAdresseonss4("adr8");
      
       DossierManagerImpl dmi = new DossierManagerImpl(em);
       dmi.insert(d);
       em.getTransaction().commit();
      
       Dossier dossier = dmi.findFromNune(d.getNune());
       assert dossier.getNune().equals(d.getNune()) : "test failed";
      
       em.close();
       }
      
       public void testFindAll() throws Exception {
       EntityManager em = EmbeddedEjbHandler.getInstance().getEntityManager();
       em.getTransaction().begin();
      
       System.out.println("testFindAll()");
       DossierManagerImpl dmi = new DossierManagerImpl(em);
       List<Dossier> list = dmi.findAll();
       assert list != null && !list.isEmpty();
      
       em.getTransaction().commit();
       em.close();
       }
      
       public void testUpdate() {
       EntityManager em = EmbeddedEjbHandler.getInstance().getEntityManager();
       em.getTransaction().begin();
      
       System.out.println("testUpdate()");
       DossierManagerImpl dmi = new DossierManagerImpl(em);
       Dossier dossier = dmi.findFromNune("1234567890");
      
       // update
       dossier.setAdresseonss1("mon adresse");
       em.merge(dossier);
       em.getTransaction().commit();
      
       // reload & check
       dossier = dmi.findFromNune("1234567890");
       assert dossier.getAdresseonss1().equals("mon adresse");
      
       em.close();
       }
      }
      



      Than, my questions are :

      1) how can i inject a reference to a logger in the session bean.
      what is the best practice

      2) in my session bean, i must add a constuctor for giving the entity manager and that only for test. Is there an alternative to do that ?

      Thanks.