← All languages

Servirtium for

Scala

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). Scala reaches libservirtium_vcr through the Java binding's jar; the module adds Servirtium.playback / Servirtium.record taking a Builder => Unit configure function.
Toolchain
JDK 22+; Scala 3.3.4 (scala.version in scala/pom.xml; Scala 3 indentation syntax), Maven or sbt
Distribution
Vendor: install the release jar, then build the Scala layer (dev.servirtium:vcr-scala) 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
scala/ · README

Getting it

There is no published package for Scala. 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 Scala layer is NOT a release asset. Build and install it from the same tag into ~/.m2 as dev.servirtium:vcr-scala (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/scala install
    mvn -q dependency:get -Dartifact=dev.servirtium:vcr-scala:2.0.0-alpha.20 && echo OK
  3. Depend on dev.servirtium:vcr-scala:2.0.0-alpha.20 from ~/.m2 (use mavenLocal() / Resolver.mavenLocal where your build tool needs telling).
    <dependency>
      <groupId>dev.servirtium</groupId>
      <artifactId>vcr-scala</artifactId>
      <version>2.0.0-alpha.20</version>
      <scope>test</scope>
    </dependency>
    
    // build.sbt
    resolvers += Resolver.mavenLocal
    libraryDependencies += "dev.servirtium" % "vcr-scala" % "2.0.0-alpha.20" % Test
  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.scala.Servirtium
import java.net.URI
import java.net.http.*

val vcr = Servirtium.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
  case 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.scala.Servirtium.playback
import dev.servirtium.vcr.Outcome
import java.net.URI
import java.net.http.*

val vcr = playback("tapes/climate_api.md")(_.port(0))
try
  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
finally vcr.close()

Idiomatic extras

Volatile values

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

Servirtium.playback("tape.md")(b => {
  b.volatileRule("response headers", "Date: {{serverDate}}")
  b.bind("serverDate", "Fri, 13 Nov 2026 09:41:07 GMT")
})  // server.volatileReport() says what was bound

Stable order and playback range

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

val vcr = Servirtium.record(tape, upstream)(
  _.prettyJson(2)                            // required for the JSON rules
    .sortJsonKeys()
    .sortJsonArray("versions", "/created")
    .sortResponseHeaders()
    .sortHeaderValue("Allow"))

val slice = Servirtium.playback(tape)(_.playbackRange(4, 6))   // interactions 4..6 only

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).
  • Scala has no receiver lambdas: configure is an ordinary Builder => Unit argument (the helpers are Servirtium.playback / Servirtium.record).

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your Scala 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 Scala 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 Scala. Follow https://servirtium.dev/languages/scala/ 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 Scala layer (dev.servirtium:vcr-scala) from the tag; the layer is not a release asset.
   Toolchain and minimum version: JDK 22+; Scala 3.3.4 (scala.version in scala/pom.xml; Scala 3 indentation syntax), Maven or sbt.
   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 Scala 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 Scala 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).
- Scala has no receiver lambdas: configure is an ordinary Builder => Unit argument (the helpers are Servirtium.playback / Servirtium.record).

References:
- This language's page: https://servirtium.dev/languages/scala/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/scala
- 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 and ran scala/example (mvn test: 1 passed). The record snippet mirrors ServirtiumScalaTest 'recordThenReplayRoundTripsViaTheScalaHelpers', which passed in the layer's own mvn test (its other errors need aeb-built fixtures under target/test-tools). The snippets as written were not compiled separately (no standalone scala CLI); volatile/sort snippets mirror the README and tests.

They mirror these files in the repository:

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