1

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

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

这是粗略的项目结构:

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
4

2 回答 2

1

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

@BeforeClass
public static void setup() {
    em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager();
}
于 2012-08-28T14:06:15.657 回答
0

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

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

于 2011-07-28T09:43:00.947 回答