抱怨没有收到异常似乎很奇怪,但我们开始吧:
待测类:
/**
* Implementation of {@link IGlossaryDataAccessObject} using JPA.
*/
@ApplicationScoped
public class JpaGlossaryDataAccessObject implements IGlossaryDataAccessObject {
@PersistenceContext
private EntityManager entityManager;
/*
* (non-Javadoc)
*
* @see
* ch.diction.webportal.glossary.model.IGlossaryDataAccessObject#createGlossary
* (java.lang.String)
*/
@Override
@TransactionAttribute
public void createGlossary(String glossaryName) {
final GlossaryColumn column = new GlossaryColumn(DEFAULT_GLOSSARY_COLUMN_NAME);
final GlossaryEntry entry = new GlossaryEntry(DEFAULT_ENTRY_VALUE, column);
final GlossaryRow row = new GlossaryRow(entry);
final Glossary glossary = new Glossary(glossaryName, row);
entityManager.persist(glossary);
}
// ...
}
单元测试:
@RunWith(Arquillian.class)
public class JpaGlossaryDataAccessObjectTest {
@Deployment
public static Archive<?> createDeployment() {
final MavenDependencyResolver resolver = DependencyResolvers.use(MavenDependencyResolver.class)
.loadMetadataFromPom("pom.xml");
return ShrinkWrap
.create(WebArchive.class, "test.war")
.addPackage(Glossary.class.getPackage())
.addPackage(IdentityObject.class.getPackage())
.addPackage(IGlossaryDataAccessObject.class.getPackage())
.addPackage(EntityManagerProducer.class.getPackage())
.addAsLibraries(resolver.artifact("org.jboss.seam.persistence:seam-persistence").resolveAsFiles())
.addAsWebInfResource("ch/diction/webportal/resources/glossary/model/beans.txt", "beans.xml")
.addAsResource("ch/diction/webportal/resources/persistence/persistence.txt", "META-INF/persistence.xml");
}
@Inject
private IGlossaryDataAccessObject dao;
@PersistenceContext
private EntityManager em;
@Test
public void testCreateGlossary() {
final String glossaryName = "empty";
dao.createGlossary(glossaryName);
final List<Glossary> result = em.createQuery("SELECT g FROM Glossary g", Glossary.class).getResultList();
Assert.assertEquals(1, result.size());
}
}
这个单元测试在我的本地 GlassFish 3.1.2.2 服务器上使用 Arquillian 运行,完成时没有任何错误或异常。然而,当我使用调试器并在“dao.createGlossary(...)”之后停止执行时,我可以使用 MySQL Workbench 验证实际上没有数据写入表。
我想知道 Arquillian 框架是否做得比我知道的要多,它可能提供某种数据库测试容器,在每次测试后自动回滚我的事务。是这种情况还是什么可能阻止数据实际写入MySQL?
感谢您的任何提示和最好的问候帕斯卡
PS:为了完整起见,这是我的其他测试配置文件:
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="web-portal-test" transaction-type="JTA">
<jta-data-source>jdbc/web-portal-test</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>
豆类.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="urn:java:ee"
xmlns:security="urn:java:org.jboss.seam.security" xmlns:permission="urn:java:org.jboss.seam.security.permission"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd">
<alternatives />
<decorators />
<interceptors>
<class>org.jboss.seam.transaction.TransactionInterceptor</class>
</interceptors>
</beans>
arquillian.xml:
<?xml version="1.0"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<defaultProtocol type="Servlet 3.0" />
<container qualifier="remote-glassfish" default="true">
<configuration>
<property name="adminUser">admin</property>
<property name="adminPassword">******</property>
<property name="adminPort">4848</property>
<property name="adminHttps">false</property>
<property name="remoteServerHttpPort">9005</property>
<property name="target">server</property>
</configuration>
</container>
</arquillian>
更新:我已经能够验证实体确实存储在实时环境中的数据库中。所以似乎 Arquillian 确实在单元测试环境中回滚了我的事务——这很奇怪,因为在他们的网站指南中,他们使用手动 SQL 命令来清理数据库: http ://arquillian.org/guides/testing_java_persistence/