Mock ChatModel
Overview
The mock chat model enables deterministic testing of LangChain4j features such as agents, tools, and chat memory without invoking an external AI service. By configuring rule patterns, fixed responses, and templated replies, tests remain reproducible and stable across runs, allowing developers to verify interaction logic, component chaining, and error handling in isolation.
Maven Coordinates
In addition to the Helidon integration with LangChain4J core dependencies, you must add the following:
<dependency>
<groupId>io.helidon.integrations.langchain4j.providers</groupId>
<artifactId>helidon-integrations-langchain4j-providers-mock</artifactId>
</dependency>
MockChatModel
To automatically create and add MockChatModel to the service registry add the
following lines to application.yaml:
To configure MockChatModel to be used, for example, in a test scenario you
define your model in application.yaml and override a chat model name
configured by @Ai.ChatModel annotation in FoodExpertAiService:
The final unit test would look like the following snippet.
@Testing.Test
class FoodExpertTest {
@Test
void customMockResponse(FoodExpertAiService aiService) {
assertThat(aiService.chat("I can prepare pizza with ananas!"), is("Don't!"));
assertThat(aiService.chat("Return this message: 'test-message'"), is("The message is: test-message"));
}
}
It is possible to inject a mock model and amend the rule programmatically.
@Testing.Test
class FoodExpertTest {
@Test
void customMockResponse(FoodExpertAiService aiService, @Service.Named("test-mock-model") MockChatModel mockModel) {
try {
mockModel.activeRules().add(new MockChatRule() {
@Override
public boolean matches(ChatRequest req) {
return true;
}
@Override
public String mock(String concatenatedReq) {
return "Custom manually added response!";
}
});
assertThat(aiService.chat("I can prepare pizza with ananas!"), is("Custom manually added response!"));
} finally {
mockModel.resetRules();
}
}
}
Configuration options
| Key | Type | Default | Description |
|---|---|---|---|
rules | List< | The list of Mocks that the mock chat model evaluates | |
enabled | Boolean | true | If set to false , MockChatModel will not be available even if configured |