gpt4 book ai didi

unit-testing - Playframework 2.2.x + Java +JPA - 用于单元测试和生产的单独数据库

转载 作者:行者123 更新时间:2023-12-04 04:43:24 26 4
gpt4 key购买 nike

我正在使用 Java + Playframework + JPA 使用 PlayFramework 2.3-M1 来开发应用程序。

我想在运行应用程序时使用 MySQL 数据库,但是当我运行测试时使用

activator test

我想在内存数据库 H2 中使用。

如何才能做到这一点 ?

下面是我的

conf/META-INF/persistence.xml
<persistence 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"
version="2.0">

<persistence-unit name="prod" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>prodDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>
</properties>
</persistence-unit>

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>TestDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>
</properties>
</persistence-unit>

</persistence>

配置文件/应用程序.conf
# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
db.test.driver=org.h2.Driver
db.test.url="jdbc:h2:mem:play"
db.test.user=sa
db.test.password=""

db.test.jndiName=TestDS

jpa.test=testPersistenceUnit

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://X.X.X.X/lb"
db.default.user=lb
db.default.password="lb"

#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS


jpa.default=defaultPersistenceUnit


# Evolutions
# ~~~~~
# You can disable evolutions if needed
evolutionplugin=disabled

# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
#
ebean.default="models.*"

这是完成它的最佳方法吗?

谢谢

最佳答案

所以我使用了一些混合/匹配方法来进行更快的测试

  • 我使用单独的配置文件进行测试和生产
  • 我为生产和测试使用了两种不同的 JPA 持久性单元
  • 在 build.sbt
  • 中添加了覆盖测试执行的配置文件
  • 用于生成测试模式
  • 利用进化将测试数据夹具加载到测试数据中

  • 以下是我的文件:

    conf/test.conf
    db.default.driver=org.h2.Driver
    db.default.url="jdbc:h2:mem:lb"
    db.default.user=lb
    db.default.password=""

    #
    # You can expose this datasource via JNDI if needed (Useful for JPA)
    db.default.jndiName=DefaultDS


    jpa.default=testPersistenceUnit


    # Evolutions
    # ~~~~~
    # You can disable evolutions if needed
    #evolutionplugin=disabled

    配置文件/应用程序.conf
    db.default.driver=com.mysql.jdbc.Driver
    db.default.url="jdbc:mysql://X.X.X.X/lb"
    db.default.user=lb
    db.default.password="lb"

    #
    # You can expose this datasource via JNDI if needed (Useful for JPA)
    db.default.jndiName=DefaultDS

    jpa.default=defaultPersistenceUnit

    # Evolutions
    # ~~~~~
    # You can disable evolutions if needed
    evolutionplugin=disabled

    然后将以下选项添加到 build.sbt 文件

    生成.sbt
    javaOptions in Test += "-Dconfig.file=conf/test.conf"

    最后是persistence.xml文件
    <persistence 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"
    version="2.0">

    <!--Persistence Unit for Production-->

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>DefaultDS</non-jta-data-source>
    <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.show_sql" value="true"></property>
    <property name="hibernate.format_sql" value="true"></property>
    </properties>
    </persistence-unit>

    <!--Persistence Unit for Testing-->

    <persistence-unit name="testPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>DefaultDS</non-jta-data-source>
    <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.show_sql" value="false"></property>
    <property name="hibernate.format_sql" value="true"></property>
    </properties>
    </persistence-unit>
    </persistence>

    最后我测试它使用
    activator test

    感谢 play-framework google group 的用户为实现这一目标提供了线索。

    关于unit-testing - Playframework 2.2.x + Java +JPA - 用于单元测试和生产的单独数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23298678/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com