【问题标题】:How to view the inserted rows when JPA testing with arquillian / hibernate?使用 arquillian / hibernate 进行 JPA 测试时如何查看插入的行?
【发布时间】:2013-05-24 22:21:56
【问题描述】:

我有一个正在编写注释并通过单元测试的 arquillian 单元测试。

现在我想实际查看保存到我的 SQLServer 数据库中的内容。

当我打开 SQLServer 时,我看到了我的“注释”表,其中包含所有必需的列……但是没有数据。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="noteUnit"
        transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/jdbc/datasources/notes</jta-data-source>
        <properties>
            <property name="hibernate.show_sql" value="true" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2005Dialect" />
            <property name="hibernate.transaction.manager_lookup_class"
                value="org.hibernate.transaction.JBossTransactionManagerLookup" />
            <property name="hibernate.hbm2ddl.auto" value="create"/>
        </properties>
    </persistence-unit>
</persistence>

我尝试了 hbm22ddl.auto--'create-drop'、'update'、'validate' 的各种值,但由于我的测试通过,我假设新行正在被插入,然后被 arquillian 立即删除单元测试后?

下面的单元测试通过——意味着 arquillian 的 xml 文件和所有其他各种管道似乎都设置正确。是否有设置可以保存所有插入的数据?

    private NoteEntity createNote(){

        NoteEntity note = new NoteEntity();
        note.setGuid("123456789");
        note.setAuthorId("12345");

        return note;
    }

    @Test
    public void createNoteTest(){
        NoteEntity note1 = createNote();
        mEntityManager.persist(note1);
        Assert.assertNotNull(note1.getId());
    }

【问题讨论】:

    标签: sql-server hibernate jpa


    【解决方案1】:

    通常,jUnit 被配置为使其事务处于defaultrollback=true 模式。这样做是为了避免在数据库中插入测试数据。您可能会在类定义或扩展类中找到配置。

    带有 Spring IOC 配置的 jUnit 示例:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath*:myPath/spring*Context.xml")
    @TransactionConfiguration(defaultRollback = true)
    @Transactional
    public abstract class AbstactSpringTestCase {
     ...
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-20
      • 1970-01-01
      • 2017-05-04
      • 2013-12-15
      • 2016-05-06
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 2012-08-22
      相关资源
      最近更新 更多