AI / LangChain4j interview questions
How do you test LangChain4j AI Services without making real LLM API calls?
Testing AI Services without hitting real LLM endpoints is essential for fast, cost-free, deterministic unit tests. LangChain4j supports this through mock model implementations and the AiServices builder accepting any ChatLanguageModel — including test doubles you create yourself.
The most direct approach is to implement a simple mock that returns predetermined responses:
// Simple lambda mock ChatLanguageModel mockModel = (messages, toolSpecifications) -> new AiMessage("The capital of France is Paris."); GeographyAssistant assistant = AiServices.builder(GeographyAssistant.class) .chatLanguageModel(mockModel) .build(); String answer = assistant.ask("What is the capital of France?"); assertThat(answer).isEqualTo("The capital of France is Paris.");
For more complex scenarios, Mockito works naturally since ChatLanguageModel is an interface:
@ExtendWith(MockitoExtension.class) class TranslatorTest { @Mock ChatLanguageModel mockModel; @Test void translatesText() { AiMessage fakeResponse = new AiMessage("Bonjour le monde"); when(mockModel.generate(anyList())).thenReturn(new Response<>(fakeResponse)); Translator translator = AiServices.builder(Translator.class) .chatLanguageModel(mockModel).build(); assertThat(translator.translate("Hello world", "French")) .isEqualTo("Bonjour le monde"); } }
For integration tests that require a real LLM but want cost control, use Ollama with a small local model (e.g., tinyllama) via Testcontainers. This gives you real model behavior without OpenAI billing and can run in CI pipelines. The langchain4j-ollama module combined with the Testcontainers Ollama image enables fully automated integration test suites with no API keys required.
More Related questions...