Anthropic Java SDK

Use AI-Mocks Anthropic with the official Anthropic Java SDK for deterministic Messages API and streaming integration tests.

Use AI-Mocks Anthropic when production code calls the official Anthropic Java SDK. Point the real SDK client at anthropic.baseUrl() so tests execute provider-shaped HTTP and streaming requests locally.

1Integration test -> Anthropic Java SDK -> AI-Mocks Anthropic -> Mokksy HTTP/SSE server

The official SDK API shapes shown below are backed by Anthropic SDK integration tests. This repository currently verifies the official Anthropic SDK from Kotlin; it also verifies LangChain4j usage separately.

Configure the client

The SDK requires an API key value during construction. Because baseUrl() routes the request to the local mock server, use a dummy value in tests rather than a live provider credential.

 1import com.anthropic.client.AnthropicClient
 2import com.anthropic.client.okhttp.AnthropicOkHttpClient
 3import dev.mokksy.aimocks.anthropic.MockAnthropic
 4
 5val anthropic = MockAnthropic(verbose = true)
 6
 7val client: AnthropicClient =
 8  AnthropicOkHttpClient.builder()
 9    .apiKey("dummy-key-for-tests")
10    .baseUrl(anthropic.baseUrl())
11    .responseValidation(true)
12    .build()

Test the Messages API

Register the request criteria and deterministic reply before invoking client.messages().create(...).

 1import com.anthropic.models.messages.MessageCreateParams
 2import io.kotest.matchers.shouldBe
 3import kotlin.jvm.optionals.getOrNull
 4
 5anthropic.messages {
 6  model = "claude-3-7-sonnet-latest"
 7  maxTokens = 100
 8  systemMessageContains("helpful assistant")
 9  userMessageContains("say 'Hello!'")
10} responds {
11  messageId = "msg_test"
12  assistantContent = "Hello"
13}
14
15val params =
16  MessageCreateParams.builder()
17    .model("claude-3-7-sonnet-latest")
18    .maxTokens(100)
19    .system("You are a helpful assistant.")
20    .addUserMessage("Just say 'Hello!' and nothing else")
21    .build()
22
23val result = client.messages().create(params)
24result.content().mapNotNull { it.text().getOrNull() }.first().text() shouldBe "Hello"

Test streaming Messages

The official SDK tests also configure streamed message content and consume it through client.messages().createStreaming(...):

 1import com.anthropic.models.messages.MessageCreateParams
 2import io.kotest.matchers.collections.shouldContainExactly
 3import kotlin.time.Duration.Companion.milliseconds
 4
 5val tokens = listOf("All", " we", " need", " is", " Love")
 6
 7anthropic.messages {
 8  model = "claude-3-7-sonnet-latest"
 9  userMessageContains("What do we need?")
10} respondsStream {
11  responseChunks = tokens
12  delay = 50.milliseconds
13  delayBetweenChunks = 10.milliseconds
14  stopReason = "end_turn"
15}
16
17val params =
18  MessageCreateParams.builder()
19    .model("claude-3-7-sonnet-latest")
20    .maxTokens(100)
21    .addUserMessage("What do we need?")
22    .build()
23
24val received = mutableListOf<String>()
25client.messages().createStreaming(params).use { response ->
26  response.stream()
27    .filter { it.isContentBlockDelta() }
28    .forEachOrdered { chunk ->
29      received += chunk.asContentBlockDelta().delta().asText().text()
30    }
31}
32
33received shouldContainExactly tokens

Next steps