【问题标题】:How can I exclude one persistence.xml from the test classpath in Eclipse without using Maven/Ant?如何在不使用 Maven/Ant 的情况下从 Eclipse 的测试类路径中排除一个 persistence.xml?
【发布时间】:2011-07-28 09:34:48
【问题描述】:

在 Eclipse 中运行 JUnit 集成测试时,我在类路径中有两个 persistence.xml 文件——一个包含测试用例的配置,另一个包含生产环境。我需要找到一种方法从测试类路径中排除用于生产的 persistence.xml。

约束:我不允许使用 Maven 或 Ant,只能使用 Eclipse Helios SR1(带有 JUnit)。 JPA 2 持久性提供程序是 EclipseLink 2.2.0。由于我必须使用 ClearCase 7.0 作为 VCS,因此无法链接资源,例如 as described here

这是粗略的项目结构:

project-datamodel (containing the Entity beans)
\- src/resources/META-INF/persistence.xml (for production; want to exclude it)
\- test/resources/META-INF/persistence.xml (for test)

project-service (containing the Session beans)
\- ejbModule

project-service-it (containing the JUnit integration tests for the Session beans)
\- test

【问题讨论】:

    标签: eclipse unit-testing maven classpath persistence.xml


    【解决方案1】:

    我遇到了这个问题,因为我的两个 persistence.xml 文件(生产和测试)使用了相同的 <persistence-unit> 名称。如上所述,类路径顺序无关紧要,因为 EclipseLink 显然会在决定使用哪个文件之前找到所有 persistence.xml 文件。我通过重命名 xml 文件和单元测试中的调用中的测试 PU 来解决它:

    @BeforeClass
    public static void setup() {
        em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager();
    }
    

    【讨论】:

      【解决方案2】:

      您可以在为单元测试创​​建运行配置时编辑类路径(如果您从上下文菜单中选择“Run As -> Junit Test”,Eclipse 将自动创建一个。

      编辑新的运行配置时,您可以在“类路径”选项卡上添加自定义文件夹,并将它们上移到默认类路径的前面。我没有尝试过,但应该可以,因为在测试中它会首先找到测试persistence.xml

      【讨论】:

      • 谢谢丹尼尔的回答!问题不在于类路径顺序,因为我猜对了,而是以下问题:由于可以有更多的持久性单元,EclipseLink 处理类路径中的所有 persistence.xml 文件。就我而言,两个 persistence.xml 文件中的持久性单元是相同的。我不知道 EclipseLink,但它似乎合并/覆盖了两个文件中的配置,从而导致了恼人的错误。这就是为什么我只想将persistence.xml 排除在生产环境之外。
      • 我之前的假设当然是错误的。对于当前的项目结构,类路径中不能有两个 persistence.xml 文件,因为 Eclipse 通过将第一个(生产)文件复制到目标目录来简单地用第二个文件(测试)覆盖它。也就是说,因为 src/resources 目录列在 test/resources 目录之前。所以使用这种结构,我应该有反之亦然的问题:生产配置不再可能。
      • 我移动了 persistence.xml 文件如下:project-service:ejbModule/META-INF/persistence.xml(用于生产)project-service-it:@ 987654323@(用于测试)现在,我又回到了起点……类路径排序没有任何改变:(。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 2011-03-16
      相关资源
      最近更新 更多