Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DeltaSpike测试控件-未找到持久性单元_Java_Unit Testing_Cdi_Deltaspike_Deltaspike Jpa - Fatal编程技术网

Java DeltaSpike测试控件-未找到持久性单元

Java DeltaSpike测试控件-未找到持久性单元,java,unit-testing,cdi,deltaspike,deltaspike-jpa,Java,Unit Testing,Cdi,Deltaspike,Deltaspike Jpa,我正在尝试使用DeltaSpike测试控制模块为我正在开发的新maven应用程序创建集成测试 它正在一个Wildfly 9实例上部署 一个简单的测试用例: @RunWith(CdiTestRunner.class) @TestControl(projectStage = ProjectStage.IntegrationTest.class) public class EmpresaCrudIntegrationTestCase { private static final String

我正在尝试使用DeltaSpike测试控制模块为我正在开发的新maven应用程序创建集成测试

它正在一个Wildfly 9实例上部署

一个简单的测试用例:

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

}
我在
src/test/resources/META-INF
中有一个
persistence.xml
,看起来像这样:

<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>
问题是创建
EntityManager


欢迎提供任何帮助。

您不能在(Java EE)容器外运行的测试中使用@PersistenceUnit注入非托管(资源\本地)entityManager。您需要查找,下面是一个示例:

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

}

另外,在
src/test/resources/META-INF

中,您还需要一个beans.xml,最突出的一点是您引用的是一个数据源。DS测试控件不像arquillian那样在完整的应用服务器中运行,它在纯CDI运行时中运行。没有可用的数据源。您需要覆盖entity manager工厂,以便根据专用数据库加载它。本节可能会给您一些提示:
@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();
    }
  }

}