Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arquillian fails to inject dependencies after the first test class

I have a slightly weird problem. I'm currently using Arquillian (1.1.0.Final) together with Embedded GlassFish (3.1.2.2). I used following guide to setup my little test project. Everything worked fine with the integrated Derby database. My real application uses PostgreSQL as a database, so I configured my GlassFish resources as follows:

    <!-- 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>

And I access the persistence context and the user transaction as described in above guide:

@RunWith(Arquillian.class)
public class AddressModuleTest 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 ...]
}

If I run my test class (AddressModuleTest, please note that "BaseTest" has a static method annotated with @Deployment for Arquillian) everything is fine and I can read my data from the PostgreSQL database.

Unfortunately, If I create a second test class it won't work:

@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 (respectively surefire) gives me the following error:

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 ... ]

.. with the following root exception:

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

I went back to the guide and tried to reproduce my issue with their code - with no sucess (i.e. they are not affected by my problem). Therefore I assume that either my UserTransaction handling is wrong or it is an issue with my PostgreSQL configuration. I tried different datasources, namely

  • org.postgresql.ds.PGSimpleDataSource
  • org.postgresql.ds.PGPoolingDataSource

As well as javax.xa.XADataSource together with org.postgresql.xa.PGXADataSource with no success.

Does somebody have a clue what's wrong? I have to admit that I'm (very) inexperience with different DataSources as well as Transaction Management.


UPDATE

It looks as if the issue is independent of PostgreSQL as it happens on MySQL (MariaDB) as well. The stacktrace is the same, so I assume the issue lies within my Transaction Management..


Kind regards and many thanks for your help

stupidSheep

like image 873
stupidSheep Avatar asked Dec 20 '22 02:12

stupidSheep


1 Answers

This bug was introduced by ARQ-1071 in 1.0.4.Final and persists in current version (1.1.1.Final). The reason is that all ThreadLocal occurences were replaced by InheritableThreadLocal to fix a NPE on @Timeout usages.

The proposed fix consists on revert just one occurence of InheritableThreadLocal to ThreadLocal like it has been done on following Pull Request: https://github.com/arquillian/arquillian-core/pull/53

Please, vote on the issue to be released on next 1.1.2.Final version: https://issues.jboss.org/browse/ARQ-1458

like image 111
zyc Avatar answered Dec 22 '22 15:12

zyc