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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
