JUnit 5 Testing with Infinispan Server Extension
What You Will Learn
How to write JUnit 5 integration tests for Infinispan using the InfinispanServerExtension, which automatically starts an Infinispan Server via Testcontainers.
Prerequisites
-
Java 17+
-
Docker or Podman available for Testcontainers
Step 1: Add the Test Driver Dependency
Add the Infinispan JUnit 5 test driver to your pom.xml:
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-server-testdriver-junit5</artifactId>
<version>${version.infinispan}</version>
<scope>test</scope>
</dependency>
Step 2: Create a Service to Test
Define a simple caching service that wraps a RemoteCache:
public class CachingService {
private RemoteCache<String, String> cache;
public CachingService(RemoteCache<String, String> cache) {
this.cache = cache;
}
public CachingService(RemoteCacheManager cacheManager) {
this.cache = cacheManager.administration()
.getOrCreateCache("names", DefaultTemplate.DIST_SYNC);
}
public void storeName(String id, String name) {
cache.put(id, name);
}
public boolean exists(String name) {
return cache.containsValue(name);
}
}
Step 3: Register the Extension and Write Tests
Use @RegisterExtension to start an Infinispan Server automatically, then test with a RemoteCacheManager provided by the extension:
@RegisterExtension
static InfinispanServerExtension infinispanServerExtension = buildExtension();
static InfinispanServerExtension buildExtension() {
if (Version.getUnbrandedVersion().contains("SNAPSHOT")) {
// In our dev branch, we need to build with the latest main branch image.
return InfinispanServerExtensionBuilder
.config()
.numServers(1)
.runMode(ServerRunMode.CONTAINER)
.property(TestSystemPropertyNames.INFINISPAN_TEST_SERVER_BASE_IMAGE_NAME, "quay.io/infinispan/server:16.1")
.build();
}
return InfinispanServerExtensionBuilder.server();
}
@Test
public void testUsingRemoteCacheManager(){
RemoteCacheManager remoteCacheManager = infinispanServerExtension.hotrod().createRemoteCacheManager();
// Create the CachingService passing the RemoteCacheManager
CachingService cachingService = new CachingService(remoteCacheManager);
// Use the service to store a name
cachingService.storeName("123", "Mickey");
// Assert values
assertTrue(cachingService.exists("Mickey"));
assertFalse(cachingService.exists("Donald"));
}
You can also test with a cache created directly by the extension:
@Test
public void testUsingACache(){
// Grab the cache created in the context of this test
RemoteCache<String, String> cache = infinispanServerExtension.hotrod().create();
// Put some data in the cache
cache.put("123", "Mickey");
cache.put("456", "Donald");
// Create the CachingService, using the cache created in the cycle of this test
CachingService cachingService = new CachingService(cache);
// Assert Values
assertTrue(cachingService.exists("Mickey"));
assertTrue(cachingService.exists("Donald"));
assertFalse(cachingService.exists("Minie"));
}
Step 4: Run the Tests
mvn test
The extension starts an Infinispan Server container, runs the tests, and stops the container automatically.
What’s Next
-
Get started with basic cache operations
-
Query your data with Ickle queries


