title: Injection of @PersistenceContext in CDI-Unit
body: <p>Here is the unit testing code. When we run unit test code (SampleServiceTest2); EntityManager injected in AbstractDao is always null! How can we inject em during unit test.  </p>
  
  <p>*** SampleServiceTest2.java </p>
  
  <pre><code>import javax.inject.Inject;
  
  import org.jglue.cdiunit.CdiRunner;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  
  @RunWith(CdiRunner.class)
  public class SampleServiceTest2 {
  
      @Inject SampleService greeter; 
  
      @Test
      public void testGreeter() throws Exception { 
          System.out.println("before2");
          greeter.addSampleData(new SampleDataDto(), new KullaniciDto()); 
          System.out.println("after2");
      } 
  }
  </code></pre>
  
  <p>*** SampleService.java</p>
  
  <pre><code>import javax.ejb.Stateless;
  import javax.inject.Inject;
  ....
  
  @Stateless
  @SecuredBean
  public class SampleService {
  
      @Inject 
      SampleLogic sampleLogic;
  
      @Yetki(tag="perm_add_sample_data")
      public void addSampleData(SampleDataDto data, KullaniciDto aktifKullaniciDto){
          SampleDataHelper sampleDataHelper = new SampleDataHelper();
  
          SampleData sampleData = sampleDataHelper.getEntity(data);
          KullaniciHelper kullaniciHelper = new KullaniciHelper();
  
          Kullanici kullanici = kullaniciHelper.getEntity(aktifKullaniciDto);
          sampleLogic.addData(sampleData, kullanici);
      }
  
  }
  </code></pre>
  
  <p>**** SampleLogic.java</p>
  
  <pre><code>import javax.inject.Inject;
  
  ....
  
  public class SampleLogic {
      @Inject 
      SampleDataDao sampleDataDao;
  
      public void addData(SampleData data, Kullanici kullanici) {
          addData1(data,kullanici);   
          System.out.println("SampleLogic : addData() called!");
      }
  
      public void addData1(SampleData data, Kullanici kullanici) {
          sampleDataDao.create(data, kullanici);
      }
  }
  </code></pre>
  
  <p>**** SampleDataDao.java</p>
  
  <pre><code>public class SampleDataDao extends AbstractDao&lt;SampleData&gt; {
      private static final long serialVersionUID = 1L;
  }
  </code></pre>
  
  <p>**** AbstractDao.java</p>
  
  <pre><code>public abstract class AbstractDao&lt;T extends BaseEntity&gt; implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @PersistenceContext(unitName="meopdb")
      private EntityManager em;
  
      protected EntityManager getEm() {
          return em;
      }
  
      @SuppressWarnings("rawtypes")
      private Class entityClass;
  
  
      @SuppressWarnings("rawtypes")
      private Class getEntityClass() {
          if (entityClass == null) {
              entityClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
          }
          return entityClass;
      }
  
      public T create(T t, Kullanici kullanici) {
          if (t.getId() != null) {
              throw new IllegalStateException("Create Operation: Oid should be null");
          }
          t.setId(getSeqNextValue(t));
          t.setEklemeZamani(new Timestamp(Calendar.getInstance().getTimeInMillis()));
          t.setEkleyenKullaniciId(kullanici.getId());
  
          t.setDurumId(EnumDurum.AKTIF.getValue());
  
          t = em.merge(t);
          em.flush();
          return t;
      }
  }
  </code></pre>