Jpa集成测试Persistence.xml

发布于 2024-12-04 09:34:17 字数 1246 浏览 0 评论 0原文

我正在尝试对我开发的服务执行集成测试。部分服务涉及使用 daos。此时,我需要做的是在我的设置中创建某种与持久单元的连接,然后我可以测试我的服务。

我基本上复制 persistence.xml 并将其放入 src/test/resources 并尝试

@Before
public void beginTransaction() {
  emf = Persistence.createEntityManagerFactory(dao-test");
  em = emf.createEntityManager();
}

@Test public void testAccountDonation(){

    AccountResult result = AccountService.getDonationAmount();
    Assert.assertEquals("Success", result.getResultCode());


}

无法获得驱动程序类“oracle.jdbc.driver.OracleDriver”和 URL“jdbc:oracle:thin:@data-arctichome.arcww2.com:1521:orclgdb1”的连接。您可能指定了无效的 URL。

这是我的持久性单元。

<persistence-unit name="dao-test"
    transaction-type="RESOURCE_LOCAL">

    <properties>
        <property name="openjpa.ConnectionDriverName"
            value="oracle.jdbc.driver.OracleDriver" />


        <property name="openjpa.ConnectionURL"
            value="jdbc:oracle:thin:@xxxxxxxxx:orclgdb1" />
        <property name="openjpa.ConnectionUserName" value="xxxxxx" />
        <property name="openjpa.ConnectionPassword"
            value="xxxxxx" />
    </properties>
</persistence-unit>

谢谢。

I am attempting to perform integration testing on services that I have developed. Part of the service involves using daos. At this point what I need to do is in my setup create some sort of connection to the persistent unit and then I can test my service.

I basically copy the persistence.xml and put it in src/test/resources and am trying

@Before
public void beginTransaction() {
  emf = Persistence.createEntityManagerFactory(dao-test");
  em = emf.createEntityManager();
}

@Test
public void testAccountDonation(){

    AccountResult result = AccountService.getDonationAmount();
    Assert.assertEquals("Success", result.getResultCode());


}

A connection could not be obtained for driver class "oracle.jdbc.driver.OracleDriver" and URL "jdbc:oracle:thin:@data-arctichome.arcww2.com:1521:orclgdb1". You may have specified an invalid URL.

Here is my persistence unit.

<persistence-unit name="dao-test"
    transaction-type="RESOURCE_LOCAL">

    <properties>
        <property name="openjpa.ConnectionDriverName"
            value="oracle.jdbc.driver.OracleDriver" />


        <property name="openjpa.ConnectionURL"
            value="jdbc:oracle:thin:@xxxxxxxxx:orclgdb1" />
        <property name="openjpa.ConnectionUserName" value="xxxxxx" />
        <property name="openjpa.ConnectionPassword"
            value="xxxxxx" />
    </properties>
</persistence-unit>

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-12-11 09:34:17

Spring 提供了很多单元帮助 -和集成测试,例如集成测试中应用程序上下文和事务的管理,因此我建议您不要尝试自己为代码中的测试创建所有持久性等内容,我建议您通读测试章(也许可以在'net)并使用 Spring 提供的功能。从长远来看,这会为你节省大量的时间和麻烦。

Spring offers a lot of help for unit- and integration-testing, for example management of application contexts and transactions in integration tests, so rather than trying to create all the persistence- etc. stuff yourself for the tests in code, I'd suggest you read through the testing chapter (and maybe look for some examples in the 'net) and use the functionality provided by Spring. It's going to save you a ton of time and hair-pulling in the long run.

稀香 2024-12-11 09:34:17

我对 JPA、GlassFish 等还很陌生,但就你而言,我想知道你从哪里启动数据库。对于嵌入式应用程序,我认为您需要在代码中启动它,不是吗?只是大声思考...

I'm still very new to JPA, GlassFish, etc, but in your case I wonder where you are starting the database. For embedded applications, I think you need to fire it up in the code, no? Just thinking out loud...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文