带Arquillian的JPA / PersistenceContext:实体不存储在MySQL数据库中

时间:2013-01-11 16:34:06

标签: mysql jpa glassfish-3 jta jboss-arquillian

抱怨没有收到异常似乎很奇怪,但我们走了:

待测班级:

/**
 * 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:为了完成,这是我的其他测试配置文件:

的persistence.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>

beans.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/

0 个答案:

没有答案