使用Arquillian测试JPA:运行Derby驱动程序而不是PostgreSQL

时间:2015-10-21 21:15:18

标签: java postgresql jpa jboss-arquillian

我有什么:

实体:

@Entity
public class TestEntity {
    @Id @GeneratedValue
    private Integer id;
    private String value;
    // Constructors, getters/setters
}

测试本身:

@RunWith(Arquillian.class)
public class TestEntityTest {

    @Deployment
    public static JavaArchive deploy() {
        return ShrinkWrap
                .create(JavaArchive.class)
                .addClass(TestEntity.class)
                .addAsManifestResource("META-INF/persistence.xml", "persistence.xml")
                .addAsResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml");
    }

    @PersistenceContext(name = "jpa_unit")
    private EntityManager entityManager;
    @Inject
    private UserTransaction userTransaction;

    @Test
    public void test() throws Exception {
        System.out.println(deploy().toString(true));
        assertNotNull("Entity manager must be injected", entityManager);
        assertNotNull("Transaction service must be injected", userTransaction);

        userTransaction.begin();

        TestEntity entity = new TestEntity("test value");
        entityManager.persist(entity);
        System.out.println("ID: [" + entity.getId() + "]");
        assertNotNull(entity.getId());

        userTransaction.commit();
    }
}

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test-jpa</groupId>
    <artifactId>test-jpa</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.1.10.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1204-jdbc42</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.container</groupId>
            <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
            <version>1.0.0.CR4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.main.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>3.1.2.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

META-INF / persistence.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
        version="2.1">
    <persistence-unit name="jpa_unit" transaction-type="JTA">
        <properties>
            <property name="javax.persistence.schema-generation-action"
                  value="drop-and-create"/>
            <property name="javax.persistence.schema-generation-target"
                  value="database"/>
            <property name="javax.persistence.jdbc.driver"
                  value="org.postgresql.Driver"/>
            <property name="javax.persistence.jdbc.url"
                  value="jdbc:postgresql://localhost:5432/test"/>
            <property name="javax.persistence.jdbc.user" value="postgres"/>
            <property name="javax.persistence.jdbc.password" value="postgres"/>
        </properties>
    </persistence-unit>
</persistence>

当我启动测试时,容器成功启动并尝试运行所有这些......但由于Derby错误而失败:

WARNING:
Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Schema 'POSTGRES' does not exist
Error Code: 20000
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
    bind => [2 parameters bound]
Query: DataModifyQuery(name="SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?")
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:644)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535)
    ...

那么,当我在persistence.xml中指定了PostgreSQL驱动程序时,为什么要选择Derby?

0 个答案:

没有答案