문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top