如何为集成和单元测试正确定义persistence.xml?

时间:2019-05-26 22:10:22

标签: unit-testing jdbc integration-testing persistence.xml

我暂时面临集成测试和数据库连接的问题,但仍然无法解决问题。.我敢打赌这在我的persistence.xml中,但是我找不到我所缺少的内容。 / p>

第一个persistence.xml文件位于ad-service/src/main/resources/META-INF/persistence.xml上,并且为:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="AdPU" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/adds</jta-data-source>
        <properties>
          <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.PostgreSQLDialect" />

            <property name="hibernate.hbm2ddl.charset_name" value="UTF-8"/> 
            <property name="hibernate.connection.CharSet" value="UTF-8"/>
            <property name="hibernate.connection.characterEncoding" value="UTF-8"/>
            <property name="hibernate.connection.useUnicode" value="true"/>

            <property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
        </properties>
    </persistence-unit>
</persistence>

用于测试(集成和单元测试)的第二个persistence.xml文件位于ad-service/src/test/resources/META-INF/persistence.xml上,

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="AdPUTest"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>domain.model.Ad</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:h2:mem:AdPUTestDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE" />
            <property name="javax.persistence.jdbc.user" value="ad" />
            <property name="javax.persistence.jdbc.password" value="ad" />

            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.id.new_generator_mappings" value="true" />
            <property name="hibernate.hbm2ddl.charset_name" value="UTF-8"/> 
            <property name="javax.persistence.sql-load-script-source" value="META-INF/instruments_test_data.sql"/>
        </properties>
    </persistence-unit>
</persistence>

我有一个错误:

Caused by: org.wildfly.swarm.container.DeploymentException: THORN0004: Deployment failed: {
"WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.jboss.datasources.adds"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => ["jboss.persistenceunit.\"ad-service-0.2.0-SNAPSHOT.war#AdPU\".__FIRST_PHASE__ is missing [jboss.naming.context.java.jboss.datasources.adds]",
"jboss.persistenceunit.\"ad-service-0.2.0-SNAPSHOT.war#AdPU\" is missing [jboss.naming.context.java.jboss.datasources.adds]"]
}

我仍然没有收到此错误。我想念什么?

(对于../test/../persistence.xml文件,我启发了自己:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="CounterpartyPUTest"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>domain.model.Address</class>
        <class>domain.model.Counterparty</class>
        <class>domain.model.Registration</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:h2:mem:CounterpartyDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="sa" />

            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.id.new_generator_mappings" value="true" />
            <property name="hibernate.hbm2ddl.charset_name" value="UTF-8"/> 
            <property name="javax.persistence.sql-load-script-source" value="META-INF/instruments_test_data.sql"/>
        </properties>
    </persistence-unit>
</persistence>

.../main/../persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="CounterpartyPU"
        transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/counterpartyds</jta-data-source>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.H2Dialect" />

            <property name="hibernate.hbm2ddl.charset_name" value="UTF-8"/> 
            <property name="hibernate.connection.CharSet" value="UTF-8"/>
            <property name="hibernate.connection.characterEncoding" value="UTF-8"/>
            <property name="hibernate.connection.useUnicode" value="true"/>

            <property name="javax.persistence.sql-load-script-source" value="META-INF/load_sample.sql"/>
        </properties>
    </persistence-unit>
</persistence>

我还有一个project-default.yml

thorntail:
  datasources:
    data-sources:
      adds:
        driver-name: myh2
        connection-url: jdbc:h2:mem:adDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
        user-name: ad
        password: ad 
    jdbc-drivers:
      myh2:
        driver-class-name: org.h2.Driver
        xa-datasource-name: org.h2.jdbcx.JdbcDataSource
        driver-module-name: com.h2database.h2
      postgresql:
        driver-class-name: org.postgresql.Driver
        xa-datasource-class-name: org.postgresql.xa.PGXADataSource
        driver-module-name: org.postgresql   

有什么想法吗?如果方言是PostGreSQL且我将其用作驱动程序H2(这是我从正确的文件中获取的驱动程序),我仍然无法理解。

任何帮助您变得宝贵的.. 谢谢!

0 个答案:

没有答案