← All languages

Servirtium for

Kotlin

No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.

FFI mechanism
Java binding (no second FFI). Kotlin reaches libservirtium_vcr through the Java binding's jar via seamless interop; the module adds a trailing-lambda DSL (playback { } / record { }) as inline functions over Vcr.playback / Vcr.record.
Toolchain
JDK 22+; Kotlin 2.1.20 (kotlin.version in kotlin/pom.xml), Maven or Gradle
Distribution
Vendor: install the release jar, then build the Kotlin layer (dev.servirtium:vcr-kotlin) from the tag; the layer is not a release asset
Finding the library
Same as Java: the native is bundled in the servirtium-vcr fat jar (/native/<os>-<arch>/) and extracted at run time; SERVIRTIUM_VCR_LIB overrides for platforms the jar lacks.
On GitHub
kotlin/ · README

Getting it

There is no published package for Kotlin. You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.

  1. 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
  2. The Kotlin layer is NOT a release asset. Build and install it from the same tag into ~/.m2 as dev.servirtium:vcr-kotlin (needs git, Maven and JDK 22+).
    git clone -q --depth 1 --branch v2.0.0-alpha.20 https://github.com/servirtium/servirtium-vcr.git
    mvn -q -DskipTests -f servirtium-vcr/kotlin install
    mvn -q dependency:get -Dartifact=dev.servirtium:vcr-kotlin:2.0.0-alpha.20 && echo OK
  3. Depend on dev.servirtium:vcr-kotlin:2.0.0-alpha.20 from ~/.m2 (use mavenLocal() / Resolver.mavenLocal where your build tool needs telling).
    <dependency>
      <groupId>dev.servirtium</groupId>
      <artifactId>vcr-kotlin</artifactId>
      <version>2.0.0-alpha.20</version>
      <scope>test</scope>
    </dependency>
    
    // build.gradle.kts
    repositories { mavenLocal(); mavenCentral() }
    dependencies { testImplementation("dev.servirtium:vcr-kotlin:2.0.0-alpha.20") }
  4. Run the test JVM with --enable-native-access=ALL-UNNAMED (Surefire argLine, Gradle jvmArgs, sbt javaOptions, Clojure :jvm-opts); JDK 24+ otherwise prints a restricted-method warning. No SERVIRTIUM_VCR_LIB is needed: the jar extracts its bundled native at runtime.

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.kotlin.record
import java.net.URI
import java.net.http.*

val vcr = record("tapes/climate_api.md", "https://climatedataapi.worldbank.org") {
    port(0)
    removeTransportHeaders()
    redactCredentials()
}
try {
    HttpClient.newHttpClient().send(
        HttpRequest.newBuilder(URI.create(vcr.baseUrl() + "/api/v1/countries")).build(),
        HttpResponse.BodyHandlers.ofString())
    vcr.commit()          // writes the tape
} catch (e: Throwable) {
    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.Outcome
import dev.servirtium.vcr.kotlin.playback
import java.net.URI
import java.net.http.*

playback("tapes/climate_api.md") { port(0) }.use { vcr ->
    val resp = HttpClient.newHttpClient().send(
        HttpRequest.newBuilder(URI.create(vcr.baseUrl() + "/api/v1/countries")).build(),
        HttpResponse.BodyHandlers.ofString())
    assertEquals(Outcome.OK, vcr.lastKind())   // optional: assert a clean match
}

Idiomatic extras

Volatile values

Templates with {{placeholders}} apply to record and playback alike; bind() supplies what no request carries.

playback("tape.md") {
    volatileRule("response headers", "Date: {{serverDate}}")
    bind("serverDate", "Fri, 13 Nov 2026 09:41:07 GMT")
}.use { server -> println(server.volatileReport()) }

Stable order and playback range

Record-side sorting needs prettyJson; playbackRange replays interactions 4..6 only.

record(tape, upstream) {
    prettyJson(2)                            // required for the JSON rules
    sortJsonKeys()
    sortJsonArray("versions", "/created")
    sortResponseHeaders()
    sortHeaderValue("Allow")                 // "POST, GET" -> "GET, POST"
}.use { vcr -> /* drive the SUT, then vcr.commit() */ }

playback(tape) { playbackRange(4, 6) }.use { vcr -> /* ... */ }

Gotchas

  • JDK 22 or newer; run the test JVM with --enable-native-access=ALL-UNNAMED.
  • Layers over the Java jar: install the release servirtium-vcr-2.0.0-alpha.20.jar first. The layer itself is built from the tag, not downloaded.
  • A recording is written by commit() and dropped by discard(); closing a record server with neither writes nothing and throws VcrException.
  • The full Java builder API stays available: the layer only adds idiom (java/docs/usage.md describes the semantics).
  • Kotlin's use { } closes the server: put commit() inside the block before it ends, or the close throws.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your Kotlin 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 Kotlin 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 Kotlin. Follow https://servirtium.dev/languages/kotlin/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: Vendor: install the release jar, then build the Kotlin layer (dev.servirtium:vcr-kotlin) from the tag; the layer is not a release asset.
   Toolchain and minimum version: JDK 22+; Kotlin 2.1.20 (kotlin.version in kotlin/pom.xml), Maven or Gradle.
   How the binding finds the native library: Same as Java: the native is bundled in the servirtium-vcr fat jar (/native/<os>-<arch>/) and extracted at run time; SERVIRTIUM_VCR_LIB overrides for platforms the jar lacks.
   This is a thin layer over the Java binding: install the Java jar (dev.servirtium:vcr) too, as the page says.

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Kotlin 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 Kotlin 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 or newer; run the test JVM with --enable-native-access=ALL-UNNAMED.
- Layers over the Java jar: install the release servirtium-vcr-2.0.0-alpha.20.jar first. The layer itself is built from the tag, not downloaded.
- A recording is written by commit() and dropped by discard(); closing a record server with neither writes nothing and throws VcrException.
- The full Java builder API stays available: the layer only adds idiom (java/docs/usage.md describes the semantics).
- Kotlin's use { } closes the server: put commit() inside the block before it ends, or the close throws.

References:
- This language's page: https://servirtium.dev/languages/kotlin/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/kotlin
- 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

Built the layer from the v2.0.0-alpha.20 tag on linux (cachyos, JDK 25, Maven) on top of the release jar's ~/.m2 install and ran kotlin/example (mvn test: 1 passed, playback of single_get.md). The record snippet mirrors ServirtiumKotlinTest 'record then replay round-trips via the Kotlin DSL', which passed in the layer's own mvn test (that run's other errors are tests needing aeb-built fixtures in target/test-tools, not present here). The snippets were not run as written (no kotlinc script run); volatile and sort snippets mirror the README and tests.

They mirror these files in the repository:

See also the feature reference, llms.txt and the kotlin/ directory.