Marcos J.C Kichel
Marcos J.C Kichel

Reputation: 7219

DeltaSpike Test-Control - Persistence Unit Not Found

I am trying to use DeltaSpike Test-Control Module to create integration tests for a new maven application that I am developing

It is being deployed on a Wildfly 9 instance

A simple test case:

@RunWith(CdiTestRunner.class)
@TestControl(projectStage = ProjectStage.IntegrationTest.class)
public class EmpresaCrudIntegrationTestCase {

    private static final String NOME_NOVA_EMPRESA = "teste";

    @Inject private EmpresaService empresaService;

    @Test
    public void test() {
        Empresa empresa = empresaService.save(new Empresa(NOME_NOVA_EMPRESA));

        Assert.assertNotNull(empresa.getId());
        Assert.assertEquals(NOME_NOVA_EMPRESA, empresa.getNome());
        Assert.assertTrue(empresa.getAtivo());
    }

}

I have a persistence.xml located inside src/test/resources/META-INF that looks like this:

<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="medipop" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:jboss/datasources/medipop-test</non-jta-data-source>

        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        </properties>

    </persistence-unit>
</persistence>

The entity manager is being exposed by a CDI producer method:

@PersistenceUnit
private EntityManagerFactory entityManagerFactory;

@Produces
@RequestScoped
public EntityManager create() {
    return this.entityManagerFactory.createEntityManager();
}

Problem is that the EntityManagerFactory remains null when creating the EntityManager

Any help is welcome.

Upvotes: 1

Views: 726

Answers (1)

rmpestano
rmpestano

Reputation: 888

You can't inject a non managed (RESOURCE_LOCAL) entityManager with @PersistenceUnit in a test which runs outside a (Java EE) container. You neeed to lookup, here is an example:

@ApplicationScoped
public class TestEntityManagerProducer {

  private EntityManager entityManager;
  private EntityManagerFactory emf;

  @PostConstruct
  public void initDB(){
    emf = Persistence.createEntityManagerFactory("medipop");
  }

  @Produces
  @RequestScoped
  public EntityManager produceEntityManager()
  {
    entityManager = emf.createEntityManager();
    return entityManager;
  }

  public void closeEntityManager(@Disposes EntityManager entityManager)
  {
    if (entityManager != null && entityManager.isOpen())
    {
      entityManager.close();
    }
  }

}

Also you need a beans.xml in src/test/resources/META-INF.

Upvotes: 1

Related Questions