我试图用arquillian运行我们的JPA单元测试 - 使用嵌入式野生蝇。 到目前为止,当我进行清洁和构建项目时,我已经完成了以下步骤:
现在我的问题:我在单元测试中调用entitymanager上的.create()或.delete()方法时遇到以下异常:
Caused by: javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context)
这些是我的pom.xml的重要依赖项:
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.7.Final</version>
<scope>test</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.test</groupId>
<artifactId>arquillian-test-spi</artifactId>
<version>1.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.7.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-arquillian-container-embedded</artifactId>
<version>8.2.0.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-embedded</artifactId>
<version>8.2.0.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.2.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-depchain</artifactId>
<version>2.1.1</version>
<scope>test</scope>
<type>pom</type>
</dependency>
</dependencies>
<!-- Plugins -->
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-dist</artifactId>
<version>8.2.0.Final</version>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>target</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
<execution>
<id>copy-db-driver</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.microsoft</groupId>
<artifactId>sqljdbc</artifactId>
<version>4.0.2206.100</version>
<outputDirectory>target/wildfly-8.2.0.Final/standalone/deployments</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.2.Final</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>datasource</id>
<phase>package</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<address>subsystem=datasources,data-source=tests</address>
<resources>
<resource>
<properties>
<connection-url>jdbc:sqlserver://SERVERADRESS_CENSORED;instanceName=web;databaseName=TMCDB</connection-url>
<jndi-name>java:jdbc/TMCDB</jndi-name>
<enabled>true</enabled>
<enable>true</enable>
<user-name>USER_CENSORED</user-name>
<password>PW_CENSORED</password>
<driver-name>sqljdbc-4.0.2206.100.jar</driver-name>
<use-ccm>false</use-ccm>
</properties>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
在我的persistence.xml中,我设置了transaction-type =“JTA”。
这是部件,.war文件由arquillian / shrinkwrap创建。 这个东西用Arquillian的@Deployment标签注释:
PomEquippedResolveStage loadPomFromFile = Maven.resolver().loadPomFromFile("pom.xml");
File[] asFile = loadPomFromFile.importRuntimeAndTestDependencies().resolve().withTransitivity().asFile();
//MavenStrategyStage asFile = loadPomFromFile.importRuntimeAndTestDependencies().resolve();
WebArchive webArchive = ShrinkWrap.create(WebArchive.class)
.addAsLibraries(asFile)
.addPackages(true, "de.companyXYZ")
.addPackages(true, "org.springframework.beans.factory.config")
// Adding persistence unit
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
// Add ms-sql driver for the embedded wildfly
.addAsWebInfResource("wildfly-ds-driver.xml")
// Add datasource for the embedded wildfly
.addAsWebInfResource("wildfly-ds.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
webArchive.as(ZipExporter.class).exportTo(new File("./target/test-package.war"), true);
那么,任何人都可以帮我解决这个问题吗? 我不明白为什么我得到这个例外 - 因为野蝇应该做交易管理 - 这就是我对他的期望......
答案 0 :(得分:3)
我添加,这解决了我的交易问题
import org.jboss.arquillian.transaction.api.annotation.Transactional;
@Transactional