Tobias Sarnow
Tobias Sarnow

Reputation: 1111

HowTo Test JAX-RS Application that uses Infinispan (JBoss 7 + Arquillian)

Info: My Application is a simple JAX-RS Service that stores some values in a cache provided by JBoss 7.1.

I would like to use Arquillian to call the service and test the response. But unfortunately I get this error when I try to run a test:

java.lang.IllegalArgumentException: 
Can not set org.infinispan.manager.CacheContainer field 
com.company.DataCache.container to 
org.jboss.as.clustering.infinispan.DefaultEmbeddedCacheManager

Here is my DataCache class:

@ManagedBean
public class DataCache<K, V> {

  @Resource(lookup="java:jboss/infinispan/container/hibernate")
  private CacheContainer container;
  private Cache<K, V> cache;

  @PostConstruct
  public void start() {
      this.cache = this.container.getCache();
  }

  public Cache<K, V> getCache() {
      return cache;
  }
}

My Testclass looks like that:

@RunWith(Arquillian.class)
@RunAsClient
public class SyncClientServerTest extends RbmlClientServerTest {

    @Deployment(testable = false)
    public static WebArchive createDeployment() {
        MavenDependencyResolver mvnResolver = DependencyResolvers.use(MavenDependencyResolver.class).loadMetadataFromPom("pom.xml").goOffline();

        return ShrinkWrap
            .create(WebArchive.class, "cache-service.war")
            .addPackages(true, Filters.exclude(".*ClientServerTest.*"), "com/company")
            .addAsLibraries(mvnResolver.artifact("org.infinispan:infinispan-core:5.2.0.Final").resolveAsFiles())
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }

    @Test
    public void testStatus() throws Exception {
        ClientRequest request = new ClientRequest("localhost:8080/cache-service/cache");
        request.accept(MediaType.APPLICATION_JSON_TYPE);
        request.body(MediaType.APPLICATION_JSON_TYPE, "");

        ClientResponse<String> responseObj = request.post(String.class);
        assertEquals(200, responseObj.getStatus());
    }
}

Question

All in all I want to use Arquilian for Client testing because I use a lot of Dependency Injection, maybe there is an example project out there that uses @Resource and Infinispan.

Upvotes: 1

Views: 1491

Answers (3)

Tobias Sarnow
Tobias Sarnow

Reputation: 1111

After all answers I got the answer and I just want to summarize it for other who look for the same issue.

  1. Removed the line: .addAsLibraries(mvnResolver.artifact("org.infinispan:infinispan-core:5.2.0.Final").resolveAsFiles())
  2. Add a MANIFEST.MF File to src/test/resource with the content Dependencies: org.infinispan export
  3. Add the line .addAsManifestResource("MANIFEST.MF") to ShrinkWrap
  4. Add the following to your standalone.xml/ domain.xml profile in JBoss:

    <subsystem xmlns="urn:jboss:domain:ee:1.0">
        <global-modules>
            <module name="org.infinispan" slot="main"/>
        </global-modules>
    </subsystem>
    

Upvotes: 5

roehrijn
roehrijn

Reputation: 1427

do you package any Infinispan classes together with your arquillian deployment? Infinispan is bundled with JBoss as module and should be availiable without deploying. Ambigious class names in classpath could cause that problem.

Regards

Jan

Upvotes: 1

Galder Zamarre&#241;o
Galder Zamarre&#241;o

Reputation: 5197

The cache container you're trying to plug is the 2nd level cache for Hibernate. You should not really try to get access to the underlying cache for Hibernate 2LC. If you need info about this cache, enable 2LC statistics.

If what you want to do is plug Infinispan Caches, check the Infinispan JBoss AS7 quickstart in where you can see how to define your own cache container, and plug it into your CDI application.

Upvotes: 1

Related Questions