1

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.

Marcos J.C Kichel
  • 6,887
  • 8
  • 38
  • 78
  • The one thing that stands out is that you're referring to a datasource. DS Test Control doesn't run in the full app server like arquillian does, it runs in a pure CDI runtime. There is no datasource available. You would need to override your entity manager factory to load it against a dedicated database. This section may give you some tips: http://deltaspike.apache.org/documentation/jpa.html#MultipleEntityManagers – John Ament Apr 13 '16 at 11:20

1 Answers1

1

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.

rmpestano
  • 838
  • 1
  • 8
  • 17