yegor256
yegor256

Reputation: 105073

How to inject PersistenceContext during unit testing?

This is my java class:

public class Finder {
  @PersistenceContext(unitName = "abc")
  EntityManager em;
  public boolean exists(int i) {
    return (this.em.find(Employee.class, i) != null);
  }
}

This is the unit test:

public class FinderTest {
  @Test public void testSimple() {
    Finder f = new Finder();
    assert(f.exists(1) == true);
  }
}

Testing fails with NullPointerException since Finder.em is not injected by anyone. How should I handle this situation properly? Does there any best practice exist?

Upvotes: 22

Views: 31339

Answers (3)

Ahmet Koylu
Ahmet Koylu

Reputation: 179

Create another package-private constructor and add only EntityManager then call the other constructor for default behavior.

@PersistentContext
private EntityManager entityManager;

private ManagementService managementService;

@Autowired
public SomeClass(ManagementService managementService) {
    this.managementService = managementService;
}

SomeClass(ManagementService managementService, EntityManager entityManager) {
    this(managementService);

    this.entityManager = entityManager;
}

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570365

Without a container like Spring (or something like Unitils - which is Spring based), you will have to inject the entity manager manually. In that case, you could use something like this as base class:

public abstract class JpaBaseRolledBackTestCase {
    protected static EntityManagerFactory emf;

    protected EntityManager em;

    @BeforeClass
    public static void createEntityManagerFactory() {
        emf = Persistence.createEntityManagerFactory("PetstorePu");
    }

    @AfterClass
    public static void closeEntityManagerFactory() {
        emf.close();
    }

    @Before
    public void beginTransaction() {
        em = emf.createEntityManager();
        em.getTransaction().begin();
    }

    @After
    public void rollbackTransaction() {   
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }

        if (em.isOpen()) {
            em.close();
        }
    }
}

Upvotes: 15

Richard Kettelerij
Richard Kettelerij

Reputation: 2174

It depends on what you want to test. When you have complex business logic in your Finder class you may want to mock the EntityManager - using a mocking framework like EasyMock or Mockito - in order to unit test that logic.

Now since that's not the case I suspect you want to test the persistency of the Employee entity (this is often referred to as integration testing). This requires the use of a database. To make testing easy and keep your tests portable you can use an in-memory database like HSQLDB for this purpose. In order to start HSQLDB, create a persistence context and inject this context into your Finder class it's advisable to use an IoC framework like Spring.

There are tons of tutorials on the internet that explain how to use JPA/Spring/HSQLDB. Take a look at this example project: Integration testing with Maven 2, Spring 2.5, JPA, Hibernate, and HSQLDB

Upvotes: 4

Related Questions