我正在尝试对Java EE应用程序的持久层进行单元测试。它通常在WildFly容器中运行,因此我试图配置Arquillian管理的WildFly容器,并在这种环境中运行持久性测试。但是,每当运行测试时,都会出现以下错误。
ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-4) Table "GAME" not found; SQL statement:
delete from GAME [42102-173]
我要保留的实体类是Game.java。
@Entity
@Table(name = "GAME")
public class Game implements Serializable {
private Long id;
private String title;
public Game() {}
public Game(String title) {
this.title = title;
}
@Id @GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
@Size(min = 3, max = 50)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
GamePersistenceTest.java
@RunWith(Arquillian.class)
public class GamePersistenceTest {
@Deployment
public static Archive<?> createDeployment() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(Game.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("jbossas-ds.xml");
}
private static final String[] GAME_TITLES = {
"Super Mario Brothers",
"Mario Kart",
"F-Zero"
};
@PersistenceContext
EntityManager em;
@Inject
UserTransaction utx;
// tests go here
@Before
public void preparePersistenceTest() throws Exception {
clearData();
insertData();
startTransaction();
}
@After
public void commitTransaction() throws Exception {
utx.commit();
}
@Test
public void shouldFindAllGamesUsingJpqlQuery() throws Exception {
// given
String fetchingAllGamesInJpql = "select g from Game g order by g.id";
// when
System.out.println("Selecting (using JPQL)...");
List<Game> games = em.createQuery(fetchingAllGamesInJpql, Game.class).getResultList();
// then
System.out.println("Found " + games.size() + " games (using JPQL):");
assertContainsAllGames(games);
}
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">
<!-- Sets the protocol which is how Arquillian talks and executes the tests inside the container -->
<defaultProtocol type="Servlet 3.0" />
<!-- Configuration to be used when the WildFly managed profile is active -->
<container qualifier="widlfly-managed" default="true">
<configuration>
<property name="jbossHome">${jbossHome:target/wildfly-8.1.0.Final}</property>
</configuration>
</container>
</arquillian>
jbossas-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.org/ironjacamar/schema
http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<datasource enabled="true"
jndi-name="jdbc/arquillian"
pool-name="ArquillianEmbeddedH2Pool">
<connection-url>jdbc:h2:mem:arquillian;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
</datasource>
</datasources>
test-persistence.xml
<?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="test">
<jta-data-source>jdbc/arquillian</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
pom.xml
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<versionRange>
[2.1,)
</versionRange>
<goals>
<goal>unpack</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<!-- Arquillian BOM (Bill Of Materials). -->
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>${version.org.jboss.arquillian}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- JUnit regression testing framework. -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysema.querydsl/querydsl-apt -->
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.7.4</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysema.querydsl/querydsl-jpa -->
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>3.7.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<!-- Arquillian WildFly managed profile -->
<profile>
<id>arq-wildfly-managed</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-arquillian-container-managed</artifactId>
<version>${version.org.wildfly}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/resources-jbossas-managed</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<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>${version.org.wildfly}</version>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<!-- JUnit regression testing framework. -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- JUnit Container Implementation for the Arquillian Project -->
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
为什么找不到我的桌子?如何解决此错误?