Java Arquillian无法在第一个测试类之后注入依赖项

Java Arquillian无法在第一个测试类之后注入依赖项,java,jdbc,datasource,postgresql-9.1,jboss-arquillian,Java,Jdbc,Datasource,Postgresql 9.1,Jboss Arquillian,我有点奇怪的问题。 我目前正在使用Arquillian(1.1.0.Final)和嵌入式GlassFish(3.1.2.2)。 我使用以下方法来设置我的小测试项目。 使用集成的Derby数据库,一切正常。 我的实际应用程序使用PostgreSQL作为数据库,因此我将GlassFish资源配置如下: <!-- See http://jdbc.postgresql.org/documentation/91/ds-cpds.html --> <jdbc-connect

我有点奇怪的问题。 我目前正在使用Arquillian(1.1.0.Final)和嵌入式GlassFish(3.1.2.2)。 我使用以下方法来设置我的小测试项目。 使用集成的Derby数据库,一切正常。 我的实际应用程序使用PostgreSQL作为数据库,因此我将GlassFish资源配置如下:

    <!-- See http://jdbc.postgresql.org/documentation/91/ds-cpds.html -->
    <jdbc-connection-pool name="MyPostgresqlPool"
                          res-type="javax.sql.DataSource"
                          datasource-classname="org.postgresql.ds.PGSimpleDataSource"
                          is-isolation-level-guaranteed="false">

        <property name="user" value="..." />
        <property name="databaseName" value="..." />
        <property name="password" value="..." />
        <property name="serverName" value="..." />
        <property name="portNumber" value="..." />

    </jdbc-connection-pool>
如果我运行我的测试类(AddressModuleTest,请注意“BaseTest”有一个带有@Deployment for Arquillian注释的静态方法),那么一切都很好,我可以从PostgreSQL数据库读取数据

不幸的是,如果我创建第二个测试类,它将无法工作:

@RunWith(Arquillian.class)
public class CommunicationModuleTest extends BaseTest {
    @PersistenceContext
    protected EntityManager em;

    @Inject
    protected UserTransaction utx;

    @Before
    public void setUp() throws Exception {
        utx.begin();
        em.joinTransaction();
    }

    @After
    public void tearDown() throws Exception {
        utx.rollback();
    }

    [ ... snip ... ]
}
Maven(分别是surefire)给了我以下错误:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.758 sec <<< FAILURE!
loadMessageDAO(package.CommunicationModuleTest)  Time elapsed: 0.027 sec  <<< ERROR!
java.lang.RuntimeException: Could not inject members
    at org.jboss.arquillian.testenricher.cdi.CDIInjectionEnricher.injectClass(CDIInjectionEnricher.java:135)
    at org.jboss.arquillian.testenricher.cdi.CDIInjectionEnricher.enrich(CDIInjectionEnricher.java:78)
    at org.jboss.arquillian.test.impl.TestInstanceEnricher.enrich(TestInstanceEnricher.java:52)
    at sun.reflect.GeneratedMethodAccessor67.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ ... snip ... ]
我回到指南,试图用他们的代码重现我的问题,但没有成功(即他们不受我的问题的影响)。因此,我假设要么我的UserTransaction处理错误,要么我的PostgreSQL配置有问题。 我尝试了不同的数据源,即

  • org.postgresql.ds.PGSimpleDataSource
  • org.postgresql.ds.pgpoolgDataSource
以及
javax.xa.XADataSource
org.postgresql.xa.PGXADataSource
都没有成功

有人知道怎么回事吗?我必须承认,我(非常)缺乏使用不同数据源以及事务管理的经验


更新

这个问题似乎与PostgreSQL无关,就像它发生在MySQL(MariaDB)上一样。stacktrace是相同的,因此我认为问题在于我的事务管理


亲切的问候,非常感谢你的帮助


stupidSheep

我有一个类似的问题(唯一的区别是我得到的消息是“WELD-001456参数resolvedBean不能为null”)。解析的bean存储在org.jboss.weld.manager.BeanManagerImpl类的实例中,该类在micro deployment中的所有测试之间共享。在我的例子中,失败的原因是当某个测试完成时,它的线程与链接到已清理的bean管理器的TestRunnerAdaptor的旧实例保持绑定(请参阅方法org.jboss.weld.manager.BeanManagerImpl.cleanup())。如果来自下一个微部署的某些测试将使用其中一个线程,那么它将失败。这个问题与版本>1.0.3.Final的Arquillian有关(在新版本中,字段类型org.jboss.Arquillian.junit.State.lastCreatedRunner从ThreadLocal更改为InheritableThreadlian,现在方法org.jboss.Arquillian.junit.State.isLastRunner()总是返回false)。

。唯一的方法是使用Arquillian 1.0.3.Final。

此错误由ARQ-1071在1.0.4.Final中引入,并在当前版本(1.1.1.Final)中持续存在。原因是所有ThreadLocal事件都被InheritableThreadLocal替换,以修复@Timeout usages上的NPE

建议的修复包括仅将一次出现的InheritableThreadLocal恢复为ThreadLocal,就像在以下拉取请求中所做的一样:

请就下一个1.1.2版本发布的问题进行投票。最终版本:

1.0.3.Final
中解决此问题的承诺在后续版本中恢复,直到版本
1.1.4.Final
最终解决时才恢复

<dependency>
    <groupId>org.jboss.arquillian</groupId>
    <artifactId>arquillian-bom</artifactId>
    <version>1.1.4.Final</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

org.jboss.arquillian
阿奎利安bom
1.1.4.最终版本
聚甲醛
进口

Hi postiveCoder!谢谢你的回答!我将我的版本改为Arquillian 1.0.3.Final,现在它可以工作了。我会询问阿奎利安社区(JBoss)是否能提供帮助。。(请参阅)如果我知道更多信息,请随时与您联系!无论是在SO还是在Arquillian论坛上,都没有人知道答案,所以我接受你的答案,因为它确实为我的问题提供了一个解决办法:)谢谢。似乎只有我们有这个问题嗨,好家伙!我“不接受”你的回答,因为zyc解决了这个问题(我还没有测试它,因为我目前没有在同一个项目上工作)。
Caused by: org.jboss.weld.exceptions.IllegalArgumentException: WELD-001324 Argument bean must not be null
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:678)
    at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:136)
    at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:686)
    at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:695)
    at org.jboss.weld.manager.SimpleInjectionTarget$1.proceed(SimpleInjectionTarget.java:106)
    at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:134)
    at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:46)
    at org.jboss.weld.manager.SimpleInjectionTarget.inject(SimpleInjectionTarget.java:102)
    at org.jboss.arquillian.testenricher.cdi.CDIInjectionEnricher.injectNonContextualInstance(CDIInjectionEnricher.java:145)
    at org.jboss.arquillian.testenricher.cdi.CDIInjectionEnricher.injectClass(CDIInjectionEnricher.java:125)
    ... 79 more
<dependency>
    <groupId>org.jboss.arquillian</groupId>
    <artifactId>arquillian-bom</artifactId>
    <version>1.1.4.Final</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>