OpenAI Java SDK
Use AI-Mocks OpenAI with the official openai-java SDK for deterministic chat, streaming, embeddings, moderation, and error-path integration tests.
Use AI-Mocks OpenAI when production code calls the official
openai-java SDK. Your test runs the real SDK client
against a local OpenAI-compatible endpoint by replacing only the base URL and using a dummy
credential.
1Integration test -> openai-java client -> AI-Mocks OpenAI -> Mokksy HTTP/SSE server
The examples below follow the official SDK integration tests in the AI-Mocks repository: Kotlin chat and streaming tests and the Java chat test.
Configure the client
Point the official SDK client to openai.baseUrl(). The API key is required by the SDK builder,
but no live OpenAI credential is used because requests go to the local mock server.
1import com.openai.client.OpenAIClient
2import com.openai.client.okhttp.OpenAIOkHttpClient
3import dev.mokksy.aimocks.openai.MockOpenai
4
5val openai = MockOpenai(verbose = true)
6
7val client: OpenAIClient =
8 OpenAIOkHttpClient.builder()
9 .apiKey("dummy-key-for-tests")
10 .baseUrl(openai.baseUrl())
11 .responseValidation(true)
12 .build()1import com.openai.client.OpenAIClient;
2import com.openai.client.okhttp.OpenAIOkHttpClient;
3import dev.mokksy.aimocks.openai.MockOpenai;
4
5var openai = new MockOpenai();
6OpenAIClient client = OpenAIOkHttpClient.builder()
7 .apiKey("dummy-key-for-tests")
8 .baseUrl(openai.baseUrl())
9 .build();Test a chat completion
Register the expected provider request with AI-Mocks, then make the SDK call through the configured client. The test fails if application code sends a request that does not match the stub.
1import com.openai.models.chat.completions.ChatCompletionCreateParams
2import com.openai.models.chat.completions.ChatCompletionMessageParam
3import com.openai.models.chat.completions.ChatCompletionUserMessageParam
4import io.kotest.matchers.shouldBe
5
6openai.completion {
7 model = "gpt-4o-mini"
8 userMessageContains("say 'Hello!'")
9} responds {
10 assistantContent = "Hello"
11 finishReason = "stop"
12}
13
14val params =
15 ChatCompletionCreateParams.builder()
16 .messages(
17 listOf(
18 ChatCompletionMessageParam.ofUser(
19 ChatCompletionUserMessageParam.builder()
20 .content("Just say 'Hello!' and nothing else")
21 .build()
22 )
23 )
24 )
25 .model("gpt-4o-mini")
26 .build()
27
28val result = client.chat().completions().create(params)
29result.choices().first().message().content().orElseThrow() shouldBe "Hello" 1import com.openai.core.JsonValue;
2import com.openai.models.ChatModel;
3import com.openai.models.chat.completions.ChatCompletionCreateParams;
4import com.openai.models.chat.completions.ChatCompletionMessageParam;
5import com.openai.models.chat.completions.ChatCompletionUserMessageParam;
6import java.util.List;
7import static org.assertj.core.api.Assertions.assertThat;
8
9openai.completion(req -> {
10 req.model("gpt-4o-mini");
11 req.requestBodyContains("say 'Hey!'");
12}).responds(response -> {
13 response.assistantContent("Hey!");
14 response.finishReason("stop");
15});
16
17var params = ChatCompletionCreateParams.builder()
18 .messages(List.of(ChatCompletionMessageParam.ofUser(
19 ChatCompletionUserMessageParam.builder()
20 .role(JsonValue.from("user"))
21 .content("Just say 'Hey!'").build())))
22 .model(ChatModel.GPT_4O_MINI)
23 .build();
24
25var result = client.chat().completions().create(params);
26assertThat(result.choices().get(0).message().content()).hasValue("Hey!");Test streaming behavior
The repository also tests client.chat().completions().createStreaming(...) against
openai.completion { ... } respondsStream { ... }, including delays before the first response and
between chunks. Use that path when application behavior depends on incremental delivery rather
than only the final message.
See the runnable OpenAI streaming examples for the complete Kotlin setup.
Covered provider surfaces
The AI-Mocks OpenAI integration tests exercise the official SDK with:
- Chat Completions, including streaming completions
- Responses inputs
- Embeddings
- Moderations
- HTTP error responses
Next steps
- OpenAI provider reference for the mock DSL and supported endpoint examples
- Spring AI if the SDK is hidden behind Spring AI
- LangChain4j if the SDK is hidden behind LangChain4j
- Spring Boot or Quarkus for application-level base URL configuration
- Integrations overview for all client and framework guides