在容器外部本地测试JPA

时间:2019-03-25 16:05:02

标签: jpa eclipselink persistence entitymanager

我目前正在重组一个使用JPA的项目。我已经在许多项目中实现了JPA,但是我遇到的问题在于该项目使用传统JDBC进行连接处理时设计的集成测试。我执行JPA操作时,目前拥有的方法正在注入实体管理器。我的persistence.xml文件当前指向JTA数据源,只有在部署应用程序时才能访问该数据源。对于我的集成测试,我想创建一个使用资源本地事务类型和连接池的“ test-persistence.xml”文件的EntityManagerFactory,这样我就可以在本地运行这些集成测试,而不必部署我的应用程序。实现这一目标的最佳方法是什么?我已经在这个问题上进行了广泛的研究,而我正在努力找到实现该目标的正确方法。我目前正在使用EclipseLink作为JPA提供程序。我将不胜感激。

这是我的一种JPA方法的示例:

@Default
@Dependent
public class DefaultLayoutProcedure {

@PersistenceContext(unitName = "DB")
EntityManager em;
public int getDefaultLayout(String selectedSeries, String selectedStyle, String selectedMaterial,
                            String selectedColor, double beltWidthInLinks) throws BpgDataAccessException
{
    List<DefaultLayoutEntity> defaultLayoutEntity = null;
    int defaultLayoutID = 0;
    try
    {
        Query q = em.createNamedStoredProcedureQuery("GetDefaultLayout");
        q.setParameter("p_series", selectedSeries)
                .setParameter("p_style", selectedStyle)
                .setParameter("p_material", selectedMaterial)
                .setParameter("p_color", selectedColor)
                .setParameter("p_width_divisor_in_links", beltWidthInLinks);
        defaultLayoutEntity = (List<DefaultLayoutEntity>) q.getResultList();
        for (int i = 0; i < defaultLayoutEntity.size(); i++)
        {
            defaultLayoutID = defaultLayoutEntity.get(i).getBeltLayoutID();
        }
    } catch (Exception e)
    {
        throw new BpgDataAccessException(e);
    }

    return defaultLayoutID;
}

}

这是我要为集成测试设置的“ test-persistence.xml”示例:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">

<persistence-unit name="DB" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>entity.DefaultLayoutEntity</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
        <property name="javax.persistence.jdbc.url" value="EXAMPLE-URL" />
        <property name="javax.persistence.jdbc.user" value="EXAMPLE-USER" />
        <property name="javax.persistence.jdbc.password" value="EXAMPLE-PASSWORD" />
        <property name="eclipselink.weaving" value="static" />
        <property name="eclipselink.logging.level.cache" value="FINEST" />
        <property name="eclipselink.deploy-on-startup" value="True"></property>
        <property name="eclipselink.jdbc.cache-statements" value="true"/>
        <property name="eclipselink.jdbc.bind-parameters" value="true"/>
        <property name = "eclipselink.jdbc.read-connections.max" value="5"/>
        <property name = "eclipselink.jdbc.read-connections.min" value="1"/>
        <property name = "eclipselink.jdbc.read-connections.initial" value="1"/>
        <property name = "eclipselink.jdbc.read-connections.shared" value="true"/>
        <property name = "eclipselink.jdbc.write-connections.max" value="5"/>
        <property name = "eclipselink.jdbc.write-connections.min" value="1"/>
        <property name = "eclipselink.jdbc.write-connections.initial" value="1"/>
    </properties>
</persistence-unit>

如何创建EntityManagerFactory以便在容器外部本地运行集成测试?

0 个答案:

没有答案