Servirtium for
Java
Ships a built package on the GitHub release. Release v2.0.0-alpha.20.
- FFI mechanism
- Java Foreign Function & Memory API (java.lang.foreign, Project Panama). Downcall handles into libservirtium_vcr through java.lang.foreign (final since JDK 22); no JNA, no JNI. The fat jar bundles the six natives under /native/
- / and extracts the right one at run time. - Toolchain
- JDK 22 or newer (maven.compiler.release 22 in java/pom.xml; tested on JDK 25), Maven or any JVM build tool; run with --enable-native-access=ALL-UNNAMED
- Distribution
- Ships: servirtium-vcr-2.0.0-alpha.20.jar (fat jar, six natives); install into ~/.m2 as dev.servirtium:vcr
- Finding the library
- Bundled in the jar and extracted at run time; SERVIRTIUM_VCR_LIB (a libservirtium_vcr from the release page) overrides, and is needed only for a platform the jar does not bundle (musl, FreeBSD) or a lean dev build.
- On GitHub
- java/ · README
Getting it
This language has a built artifact on the release: no source to vendor.
- Install the release fat jar (six natives bundled: linux, macOS, Windows x86_64 and arm64) into your local Maven repository as dev.servirtium:vcr. It is not on Maven Central yet, so there is nothing to fetch by coordinate.
TAG=v2.0.0-alpha.20; VER=2.0.0-alpha.20 curl -fsSLO https://github.com/servirtium/servirtium-vcr/releases/download/$TAG/servirtium-vcr-$VER.jar mvn -q install:install-file -Dfile=servirtium-vcr-$VER.jar -DgroupId=dev.servirtium -DartifactId=vcr -Dversion=$VER -Dpackaging=jar mvn -q dependency:get -Dartifact=dev.servirtium:vcr:$VER && echo OK - Add the dependency (test scope). Gradle: repositories { mavenLocal() }; sbt: Resolver.mavenLocal; deps.edn: {dev.servirtium/vcr {:mvn/version "2.0.0-alpha.20"}}.
<dependency> <groupId>dev.servirtium</groupId> <artifactId>vcr</artifactId> <version>2.0.0-alpha.20</version> <scope>test</scope> </dependency> - Run the test JVM with --enable-native-access=ALL-UNNAMED, for example in Surefire.
<plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>--enable-native-access=ALL-UNNAMED</argLine> </configuration> </plugin>
Record
Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.
import dev.servirtium.vcr.*;
import java.net.URI;
import java.net.http.*;
VcrServer vcr = Vcr.record("tapes/climate_api.md", "https://climatedataapi.worldbank.org")
.removeTransportHeaders()
.redactCredentials()
.port(0)
.start();
try {
HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
client.send(HttpRequest.newBuilder(URI.create(vcr.baseUrl() + "/api/v1/countries")).build(),
HttpResponse.BodyHandlers.ofString());
vcr.commit(); // writes the tape; without commit() or discard(), close() throws
} catch (Exception e) {
vcr.discard(); // a failing test drops the recording
throw e;
} finally {
vcr.close();
}
Playback
The same tape, replayed with no network.
import dev.servirtium.vcr.*;
import java.net.URI;
import java.net.http.*;
try (VcrServer vcr = Vcr.playback("tapes/climate_api.md").port(0).start()) {
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> r = client.send(
HttpRequest.newBuilder(URI.create(vcr.baseUrl() + "/api/v1/countries")).build(),
HttpResponse.BodyHandlers.ofString());
assertEquals(Outcome.OK, vcr.lastKind()); // optional: assert a clean match
vcr.assertNoMismatches(); // nothing was refused
vcr.assertAllConsumed(); // every recorded interaction was replayed
}
Idiomatic extras
Scrub secrets and sort for a stable re-record
Header presets plus opt-in stable order; sorted JSON is only written pretty-printed, so prettyJson is required or commit() throws. Register the same credential preset on the playback builder so a real token still matches the scrubbed tape.
Vcr.record(tape, upstream)
.removeTransportHeaders()
.redactCredentials() // Authorization, cookies, X-Api-Key, X-Auth-Token
.prettyJson(2)
.sortJsonKeys() // object keys sorted at every depth
.sortResponseHeaders() // header lines by name
.start();
Vcr.playback(tape).redactCredentials().port(0).start();
Volatile values
One rule set for record, playback and drift detection: captured ids and timestamps become {{token}}s in the tape, and playback binds what no request carries.
Vcr.record(tape, upstream)
.volatileRule("request path", "/orders/{{order}}/items")
.volatileRule("response headers", "Date: {{serverDate}}")
.start();
Vcr.playback(tape)
.bind("serverDate", "Fri, 13 Nov 2026 09:41:07 GMT")
.start();
Replay only a slice of a tape
Interaction numbers are 0-based and inclusive; a request outside the range is never served.
Vcr.playback(tape).playbackRange(4, 6).start(); // playbackRange(4) = 4 through the last
Gotchas
- JDK 22+ is required; older JDKs fail with UnsupportedClassVersionError.
- Pass --enable-native-access=ALL-UNNAMED to the test JVM, or JDK 24+ prints a restricted-method warning.
- The JDK HttpClient defaults to HTTP/2 and sends upgrade headers (HTTP2-Settings, Upgrade); under strictHeaders() build it with HttpClient.Version.HTTP_1_1 or removeHeader them.
- Closing a record server with neither commit() nor discard() writes nothing and throws VcrException.
- Not on Maven Central: install the release jar into ~/.m2 (the dev.servirtium:vcr coordinates are final).
Prompt for your coding agent
Paste this into Claude, Codex, Gemini or any coding agent, in your Java project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.
Hey Gemini or Claude or Codex,
I want a test in this Java project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.
1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.
2. Add the Servirtium binding for Java. Follow https://servirtium.dev/languages/java/ exactly. Servirtium ships a built package for this language on the GitHub release v2.0.0-alpha.20: servirtium-vcr-2.0.0-alpha.20.jar (fat jar, six natives); install into ~/.m2 as dev.servirtium:vcr.
Toolchain and minimum version: JDK 22 or newer (maven.compiler.release 22 in java/pom.xml; tested on JDK 25), Maven or any JVM build tool; run with --enable-native-access=ALL-UNNAMED.
How the binding finds the native library: Bundled in the jar and extracted at run time; SERVIRTIUM_VCR_LIB (a libservirtium_vcr from the release page) overrides, and is needed only for a platform the jar does not bundle (musl, FreeBSD) or a lean dev build.
3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Java way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.
4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for Java is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.
5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.
6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.
7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.
Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- JDK 22+ is required; older JDKs fail with UnsupportedClassVersionError.
- Pass --enable-native-access=ALL-UNNAMED to the test JVM, or JDK 24+ prints a restricted-method warning.
- The JDK HttpClient defaults to HTTP/2 and sends upgrade headers (HTTP2-Settings, Upgrade); under strictHeaders() build it with HttpClient.Version.HTTP_1_1 or removeHeader them.
- Closing a record server with neither commit() nor discard() writes nothing and throws VcrException.
- Not on Maven Central: install the release jar into ~/.m2 (the dev.servirtium:vcr coordinates are final).
References:
- This language's page: https://servirtium.dev/languages/java/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/java
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)
How these snippets were checked
Playback and record+commit+discard snippets run on macOS arm64 with openjdk 25 and the release servirtium-vcr-2.0.0-alpha.20.jar (sha256 checked against SHA256SUMS.txt) against java/example's single_get.md tape and a local JDK upstream; the extras (removeTransportHeaders, redactCredentials, prettyJson, sortJsonKeys, sortResponseHeaders, playbackRange, close without commit throwing) also run. java/example's two tests pass (mvn test, JDK 25, linux) from the same jar. volatileRule/bind mirror VolatileValuesTest and were not run by me. assertNoMismatches/assertAllConsumed are in VcrServer's source but were not run in Java (run in .NET).
They mirror these files in the repository:
java/example/src/test/java/com/example/PlaybackConsumerTest.javajava/example/src/test/java/com/example/RecordConsumerTest.javajava/src/test/java/dev/servirtium/vcr/RecordTest.javajava/src/test/java/dev/servirtium/vcr/VolatileValuesTest.java
See also the feature reference, llms.txt and the java/ directory.