在使用arquillian / hibernate进行JPA测试时,如何查看插入的行?

时间:2020-12-19 23:59:50

I have an arquillian unit test that is writing a Note and passing the unit tests.

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

Now I would like to actually view what is being persisted into my SQLServer database.

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

When I open up SQLServer, I see my "Note" table, with all of the requisite columns...but there's no data.

当我打开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>

I've tried various values for hbm22ddl.auto--'create-drop', 'update','validate', but since my test passes, I assume that the new rows are being inserted and then immediately removed by arquillian after the unit test?

我尝试过hbm22ddl的不同值。auto——'create-drop', 'update','validate',但是由于我的测试通过了,我假设在单元测试之后插入新的行,然后由arquillian立即删除它们?

Unit test below passes--meaning arquillian's xml file and all the other assorted plumbing appears to be set up correctly. Is there a setting somewhere to save all the data that's being inserted?

下面的单元测试通过——这意味着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());
    }

1 个解决方案

#1


0  

Generally, jUnit are configured so that their transaction is in defaultrollback=true mode. This is done to avoid inserting test data in your database. You will probably find the configuration over your class definition or in an extended class.

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

Example for jUnit with Spring IOC configuration :

使用Spring IOC配置的jUnit示例:

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

#1


0  

Generally, jUnit are configured so that their transaction is in defaultrollback=true mode. This is done to avoid inserting test data in your database. You will probably find the configuration over your class definition or in an extended class.

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

Example for jUnit with Spring IOC configuration :

使用Spring IOC配置的jUnit示例:

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