JPA 或 Hibernate 的测试数据生成器

发布于 2024-12-12 19:57:42 字数 271 浏览 4 评论 0原文

是否有任何工具或库可用于使用 JPA 或实体 bean 生成测试数据?我相信这对于单元测试非常有帮助,在单元测试中我们可以拥有一个内存数据库,其中的数据在我们开始测试时动态生成。因此,不会与实际的数据库服务器进行通信,也不会浪费时间。

我只能找到 JPAMock。但它仍在开发中。如果有人能提供一个好的指导那就太好了。

多谢。

Are there any tools or libraries that can be used to generate test data using JPA or entity beans? I believe this will be very helpful for unit testing where we can have an in memory database with data dynamically generated just when we start our testing. So, there will be no communication with actual DB servers and not any waste of time.

I was only able to find JPAMock. But it is still under development. It would be nice if someone could provide a good pointer.

Thanks a lot.

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

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

发布评论

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

评论(3

亚希 2024-12-19 19:57:43

您可以尝试 Random-JPA,这是一个旨在生成随机数据的框架。设置是最少的。

我开发了它,并在生产测试中使用了一年多。它工作得很好。虽然它目前支持 Oracle、MySQL 和 MySQL。具有完整功能的 MSSQL。

代码:https://github.com/kuros/random-jpa

教程:https://kuros.in/random-jpa

You could try out Random-JPA, a framework designed to generate random data. The setup is minimal.

I developed it and have been using it for over an year on our production tests. It works perfectly fine. Though it currently supports Oracle, MySQL & MSSQL with full feature.

Code: https://github.com/kuros/random-jpa

Tutorial: https://kuros.in/random-jpa

青丝拂面 2024-12-19 19:57:43

您可以使用Quickcheck 实现之一。快速检查方法独立于 JPA/Hibernate。您将基于 Quickcheck 生成域对象实例并保留它们或模拟持久层。

You could use one of the Quickcheck implementations. The quickcheck approach is independent from JPA/Hibernate. You would generate domain object instances based on Quickcheck and persist them or mock the persistence layer.

兔姬 2024-12-19 19:57:43

我将 DBUnit 与 H2 数据库以及 Untils & 结合使用。春天。工作得很好:

@SpringApplicationContext({ "classpath:test-context-dbunit.xml" })
@Transactional(TransactionMode.ROLLBACK)
@DataSet
public class ApplicationDaoTest extends UnitilsTestNG {

    @SpringBeanByType
    private ApplicationDao applicationDao;

    @Test
    public void findAll() throws Exception {
        List<Application> actual = applicationDao.findAll();
        assertNotNull(actual);
        assertEquals(actual.size(), 3);
    }
}

数据设置在 XML 文件中:

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <APPLICATION APPLICATION_ID="1" name="APP3" enabled="1" application_type="TRADE" api_key="AK1" auto_create_topics="1" />
    <APPLICATION APPLICATION_ID="2" name="APP1" enabled="1" application_type="TRADE" api_key="AK2" auto_create_topics="1" />
    <APPLICATION APPLICATION_ID="3" name="APP2" enabled="0" application_type="TRADE" api_key="AK3" auto_create_topics="1" />
</dataset> 

在 Spring 测试上下文中,您可以这样定义数据源:

<bean id="dataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean"/>

您使用的方言:org.hibernate.dialect.H2Dialect

最后在类路径中添加一个unitils.properties例如:

database.driverClassName=org.h2.Driver
database.url=jdbc:h2:mem:test
database.user=sa
database.password=
database.schemaNames=public
database.dialect=hsqldb

更新

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="..."/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
        </props>
    </property>
</bean>

I use DBUnit in conjunction with H2 database and untils & Spring. Works nicely:

@SpringApplicationContext({ "classpath:test-context-dbunit.xml" })
@Transactional(TransactionMode.ROLLBACK)
@DataSet
public class ApplicationDaoTest extends UnitilsTestNG {

    @SpringBeanByType
    private ApplicationDao applicationDao;

    @Test
    public void findAll() throws Exception {
        List<Application> actual = applicationDao.findAll();
        assertNotNull(actual);
        assertEquals(actual.size(), 3);
    }
}

The data is set in an XML file:

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <APPLICATION APPLICATION_ID="1" name="APP3" enabled="1" application_type="TRADE" api_key="AK1" auto_create_topics="1" />
    <APPLICATION APPLICATION_ID="2" name="APP1" enabled="1" application_type="TRADE" api_key="AK2" auto_create_topics="1" />
    <APPLICATION APPLICATION_ID="3" name="APP2" enabled="0" application_type="TRADE" api_key="AK3" auto_create_topics="1" />
</dataset> 

In Spring test context you define your datasource as such:

<bean id="dataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean"/>

As dialect you use: org.hibernate.dialect.H2Dialect

Finally a unitils.properties in your classpath like:

database.driverClassName=org.h2.Driver
database.url=jdbc:h2:mem:test
database.user=sa
database.password=
database.schemaNames=public
database.dialect=hsqldb

UPDATE

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="..."/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
        </props>
    </property>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文