English 中文(简体)
How to test a DAO with JPA implementation?
原标题:

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 :

public interface PersonDao
{
  public Person get(long id);
}

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 .

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);
  }
}

This is a JPA-implemented DAO , I hope the EE container or the test container able to inject the EntityManager (just like Spring does).

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);
  }
}

This is my test file .

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.

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 ?

I tried unitils.org , but cannot find a sample like this , it just directly inject the EntityManagerFactory to the TestCast , not what I want ...

最佳回答

because I came from Spring, I believe DAO still has its value, so I decided to add a DAO layer.

I don t really see what Spring has to do with this. And I don t agree as I wrote in a previous answer. To me, JPA is a DAL (data access layer), and I don t see the point of putting a data access layer on top of another data access layer. At least not systematically. But let s not discuss this.

This is a JPA-implemented DAO , I hope the EE container or the test container able to inject the EntityManager (just like Spring does).

If your DAO is a managed component like a CDI managed bean, then the Java EE container should be able to inject an EntityManager in it.

For unit testing of container-managed objects, you don t need any kind of container. For integration testing, you will need some kind of container, just like you do for Spring beans, Hibernate/JPA entities, session beans, CDI managed beans or any other kind of container-managed object. You could use the EJB3.1 embeddable API in your tests. Also have a look at Arquillian.

问题回答

You could also add method PersonDaoImpl.setEntityManager(EntityManager em), then set it by Persistence.createEntityManagerFactory("test").createEntityManager(). It s nothing with Java EE container.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签