Arquillian DBunit 事务

Arquillian DBunit Transaction

我正在使用 TomEE microprofile 和 Arquillian DBunit 进行 POC。我从 https://github.com/apache/tomee/blob/master/examples/arquillian-jpa/src/test/java/org/superbiz/arquillian/test/persistence/PersistenceTest.java

引用了程序

但我在 POC 中使用了不同的 class 和不同的方法。我得到

org.apache.openejb.OpenEJBRuntimeException: java.lang.IllegalStateException: No transaction associated with current thread
Caused by: java.lang.IllegalStateException: No transaction associated with current thread

朱特

@RunWith(Arquillian.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UserServiceTest {

    @ArquillianResource
    private URI baseURI;

    @PersistenceContext(unitName = "MICRO_DS")
    private EntityManager em;

    @Resource(name = "movieDatabase")
    DataSource dataSource;

@Deployment()
public static WebArchive createDeployment() throws IOException {
    PomEquippedResolveStage pomEquippedResolveStage = Maven.resolver().loadPomFromFile("pom.xml");

    File[] hibernateLibs = pomEquippedResolveStage.resolve(
            "org.hibernate:hibernate-entitymanager"

    ).withTransitivity().asFile();

    final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test.war")
            .addPackages(true,"ch.qos.logback")
            .addPackage(UserResource.class.getPackage())
            .addPackage(UserDE.class.getPackage())
            .addPackage(User.class.getPackage())
            .addPackage(UserDao.class.getPackage())
            .addPackage(Utils.class.getPackage())
            .addPackage(BusinessException.class.getPackage())
            .addClass(DataHelper.class)

            .addAsLibraries(hibernateLibs)
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
            .addAsResource("test-persistence.xml", "META-INF/persistence.xml")

            .addAsResource("logback.xml","logback.xml")
            .addAsResource("datasets/users.json");

    System.out.println(webArchive.toString(true));
    return webArchive;
}

    @Test
    @Transactional(TransactionMode.COMMIT)
    @UsingDataSet("datasets/users.json")
    public void test3FindAll() {
        System.out.println("##########################################");
        assertEquals(2, em.createQuery("select count(e) from UserDE e", Number.class).getSingleResult().intValue());
}

Persistence.xml

<persistence-unit name="MICRO_DS" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>movieDatabase</jta-data-source>
    <class>com.demo.entity.UserDE</class>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:test"/>
        <property name="hibernate.connection.username" value="sa"/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="jdbc.driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
    </properties>

</persistence-unit>

Arquillian.xml

<arquillian xmlns="http://jboss.org/schema/arquillian"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="
              http://jboss.org/schema/arquillian
              http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <extension qualifier="persistence">
        <property name="defaultDataSource">java:comp/env/movieDatabase</property>
    </extension>

    <container qualifier="tomee" default="true">
        <configuration>
            <property name="debug">false</property>
            <property name="httpPort">-1</property>
            <property name="ajpPort">-1</property>
            <property name="stopPort">-1</property>
            <property name="classifier">microprofile</property>
            <property name="conf">src/test/conf</property>
            <property name="dir">target/tomee</property>
            <property name="appWorkingDir">target/workdir</property>
            <property name="cleanOnStartUp">true</property>
            <property name="properties">
                app.version=900
                a.c=cat
            </property>
        </configuration>
    </container>
</arquillian>

依赖关系

    <version.arquillian>1.1.10.Final</version.arquillian>
    <version.shrinkwrap.resolvers>3.1.3</version.shrinkwrap.resolvers>
    <version.arquillian.persistence>1.0.0.Alpha7</version.arquillian.persistence>

<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.jboss.shrinkwrap.resolver</groupId>
    <artifactId>shrinkwrap-resolver-depchain</artifactId>
    <version>${version.shrinkwrap.resolvers}</version>
    <scope>test</scope>
    <type>pom</type>
</dependency>

<dependency>
    <groupId>org.jboss.arquillian.config</groupId>
    <artifactId>arquillian-config-api</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.arquillian.extension</groupId>
    <artifactId>arquillian-persistence-dbunit</artifactId>
    <version>${version.arquillian.persistence}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>arquillian-transaction-jta</artifactId>
            <groupId>org.jboss.arquillian.extension</groupId>
        </exclusion>
        <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
        </exclusion>
        <exclusion>
            <artifactId>log4j</artifactId>
            <groupId>log4j</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.apache.tomee</groupId>
    <artifactId>arquillian-tomee-remote</artifactId>
    <version>${tomee.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
        </exclusion>
    </exclusions>
</dependency>

我还没有添加数据源 属性 进行测试。适当添加后。它运作良好。更改在 arquillian.xml

<extension qualifier="persistence">
    <property name="defaultDataSource">openejb:Resource/mpdatasource</property>
    <property name="defaultTransactionMode">COMMIT</property>
    <property name="defaultCleanupPhase">NONE</property>
</extension>

<property name="properties">
                        mpdatasource=new://Resource?type=DataSource
                        mpdatasource.JdbcDriver=org.hsqldb.jdbcDriver
                        mpdatasource.JdbcUrl=jdbc:hsqldb:mem:test
                        mpdatasource.UserName=sa
                        mpdatasource.Password=
                        mpdatasource.JtaManaged=true
                        mpdatasource.LogSql=true
</property>