Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test a DAO with JPA implementation?
    text
    copied!<p>I came from the Spring camp , I don't want to use Spring , and am migrating to JavaEE6 , But I have problem testing DAO + JPA , here is my simplified sample :</p> <pre><code>public interface PersonDao { public Person get(long id); } </code></pre> <p>This is a very basic DAO , because I came from Spring , I believe DAO still has its value , so I decided to add a DAO layer .</p> <pre><code>public class PersonDaoImpl implements PersonDao , Serializable { @PersistenceContext(unitName = "test", type = PersistenceContextType.EXTENDED) EntityManager entityManager ; public PersonDaoImpl() { } @Override public Person get(long id) { return entityManager .find(Person.class , id); } } </code></pre> <p>This is a JPA-implemented DAO , I hope the EE container or the test container able to inject the EntityManager (just like Spring does).</p> <pre><code>public class PersonDaoImplTest extends TestCase { @Inject protected PersonDao personDao; @Override protected void setUp() throws Exception { //personDao = new PersonDaoImpl(); } public void testGet() { System.out.println("personDao = " + personDao); // NULL ! Person p = personDao.get(1L); System.out.println("p = " + p); } } </code></pre> <p>This is my test file .</p> <p>OK , here comes the problem : Because JUnit doesn't understand @javax.inject.Inject , the PersonDao will not be able to injected , the test will fail. </p> <p>How do I find a test framework that able to inject the EntityManager to the PersonDaoImpl , and @Inject the PersonDaoImpl to the PersonDao of TestCase ? </p> <p>I tried unitils.org , but cannot find a sample like this , it just directly inject the EntityManagerFactory to the TestCast , not what I want ...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload