Quick revision
The short answer
We test in layers. Unit tests stub the ChatModel or our own client interface, so prompt assembly, parsing, error handling and retries are tested deterministically. Integration tests run a small local model in a container, for example with Testcontainers' Ollama module, to exercise the full call path and structured output parsing. Recorded real responses are replayed to regression-test downstream logic. What a small local model cannot test is production quality, so quality evaluation runs separately against the production model on an evaluation set. Models are large images, so CI must cache them.
What to remember
In this answer, we designed a layered test strategy for a Spring Boot service using an LLM. Unit tests stub the ChatModel or our interface to test prompt assembly, parsing, errors and retries deterministically. Integration tests run a small local model with Testcontainers' Ollama module to test the full call path and structured output parsing, without asserting on quality. Record and replay covers downstream logic with realistic outputs. Quality is evaluated separately against the production model on an evaluation set, and CI caches the large model images to stay fast.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We test in layers. Unit tests stub the ChatModel or our own client interface, so prompt assembly, parsing, error handling and retries are tested deterministically. Integration tests run a small local model in a container, for example with Testcontainers' Ollama module, to exercise the full call path and structured output parsing. Recorded real responses are replayed to regression-test downstream logic. What a small local model cannot test is production quality, so quality evaluation runs separately against the production model on an evaluation set. Models are large images, so CI must cache them.
Why this question is asked
Testing code that calls an LLM is awkward: the dependency is slow, costs money, is not deterministic and may change under us. Teams often end up with either no tests or tests that call the real provider and fail randomly. A local model in CI looks like the answer, but it answers only some questions. Interviewers ask this design question to see whether we know which risks each kind of test covers, and whether we keep CI fast and reliable while still measuring what users care about. In this answer, we will lay out the test layers, write a unit test with a stubbed model, an integration test with a containerised local model, and a record-and-replay test, and then cover quality evaluation and CI resources.
1What We Need to Test
An LLM feature has two kinds of risk. The first is ordinary software risk in our code: building the prompt from the right data, escaping or delimiting untrusted input, parsing the response into our types, handling timeouts, rate-limit errors and invalid output, and what the business logic does with the result. These are deterministic once we control the model's response, and they belong in normal fast tests.
The second is model quality: is the summary correct, does the classification match, does the model follow the format with the production model and the production prompt. That depends on one specific model and cannot be tested with a stub or with a different, smaller model. Keeping these two kinds apart is the heart of the strategy.
- Quality evaluationproduction model, evaluation set, nightly or on prompt and model changes
- Record and replayreal responses stored as fixtures, downstream logic
- Integration with a local modelTestcontainers Ollama, full call path, parsing
- Unit tests with a stubbed modelprompt assembly, parsing, errors, retries
4 layers, from the top down. Step through them one at a time.
Lower layers are fast and run on every commit; the top layer measures quality.
2Unit Tests With a Stubbed Model
The base of the pyramid stubs the model. If our code depends on Spring AI's ChatModel, we can mock it with Mockito and build a ChatClient on top, so the code under test runs exactly as in production up to the model call. If we wrapped the model behind our own interface, stubbing is even simpler. These tests run in milliseconds and cover the cases we cannot reliably provoke from a real model: invalid JSON, a missing field, a timeout, a rate-limit error.
We can also capture the Prompt passed to the mock and assert on it: the system message contains the category list, the ticket text is inside the delimiters, and the temperature is 0. Those assertions catch prompt-assembly bugs that would otherwise surface as quality problems.
3Integration Tests With a Local Model in a Container
The next layer checks that the whole path works against a real model server: HTTP client configuration, serialization, streaming, the structured output conversion and our timeouts. Testcontainers has an Ollama module that starts Ollama in Docker, and Spring AI provides a service connection for it so the base URL is wired automatically (check your version's testcontainers support module). We pull a very small model, around 0.5 to 1 billion parameters, because the goal is to exercise the plumbing, not to judge quality.
Notice the comment in the test. The assertion is that the response parsed into a valid enum value, not that it is the right category. Asserting on correctness with a tiny model would make the test flaky and would test the wrong model anyway.
4Record and Replay for Downstream Logic
Some logic depends on realistic model output: rendering a summary with citations, splitting a long answer into messages, routing on a classification with confidence. For these, we record real responses from the production model once, store them as fixtures (with sensitive data removed), and replay them in tests. Replaying can happen at the HTTP level with a tool such as WireMock, or at the ChatModel level with a stub that returns stored responses.
Recorded fixtures give realistic, deterministic inputs to downstream code, and when we change the prompt or model we re-record them, which also shows in the diff how outputs changed.
5Quality Evaluation Against the Production Model
Quality is measured where it lives: with the production model and the production prompt, on an evaluation set of a few hundred real cases with expected answers or scoring rules. This run costs money and takes minutes, so it does not run on every commit. It runs nightly, and always when a pull request changes a prompt template or the configured model version, reporting accuracy or other metrics against the last accepted baseline. A drop beyond an agreed tolerance blocks the change.
| Layer | Can test | Cannot test |
|---|---|---|
| Stubbed model | Prompt assembly, parsing, errors, retries | Any real model behaviour |
| Local model in a container | HTTP path, serialization, streaming, schema parsing | Production quality or format compliance |
| Record and replay | Downstream logic on realistic outputs | Changes in today's model behaviour |
| Evaluation on production model | Accuracy, format compliance, factuality | Fast feedback on every commit |
6Keeping CI Fast and Affordable
Model files are large. Even a small model adds hundreds of megabytes, and pulling it on every CI run wastes minutes and bandwidth. We cache it: either build a custom image with the model already pulled and push it to our registry, or mount a cached volume on the CI runner. Testcontainers' Ollama module can also commit a container with a pulled model into a reusable image (check your version for the exact method).
We also keep the integration layer small, a handful of tests that cover each call path once, and mark it so it can run in a separate CI stage. CPU-only runners are enough for a 0.5B model, but generation is slow, so we keep max output tokens low in tests.
7Follow-up questions
Why not call the real provider in every CI run?
Because it makes CI slow, costly and flaky: outputs vary, rate limits and outages fail builds for reasons unrelated to the change, and secrets must be available to every build. Calling the provider belongs in the scheduled evaluation run, where variation is measured rather than treated as failure.
Can a local model be used for evaluation if production is also that model?
Yes. If we self-host the same open-weight model in production, the evaluation can run against the same model and settings, ideally on similar hardware, since quantization and settings affect outputs.
How do you test streaming endpoints?
With the stubbed model returning a Flux of chunks, using Reactor's StepVerifier to assert order, completion and error handling; then one integration test against the local model to check the real streaming path.
8Conclusion
In this answer, we designed a layered test strategy for a Spring Boot service using an LLM. Unit tests stub the ChatModel or our interface to test prompt assembly, parsing, errors and retries deterministically. Integration tests run a small local model with Testcontainers' Ollama module to test the full call path and structured output parsing, without asserting on quality. Record and replay covers downstream logic with realistic outputs. Quality is evaluated separately against the production model on an evaluation set, and CI caches the large model images to stay fast.
Open with
I separate testing our code from evaluating the model. At the bottom are unit tests that stub the model, mocking Spring AI's ChatModel or our own interface, so I can deterministically test prompt assembly, delimiting of untrusted input, parsing into our records, and handling of invalid JSON, timeouts and rate-limit errors with retries.
Then, if they want more
These run in milliseconds on every commit. Next, a small set of integration tests runs a real local model in a container: Testcontainers has an Ollama module and Spring AI can wire it with a service connection.
I use a tiny model, maybe half a billion parameters, to test the full HTTP path, streaming and structured output parsing, and I assert only that the output parses, never that the answer is right, because a small local model does not reproduce production quality.
For downstream logic that needs realistic outputs, I record real production-model responses as fixtures and replay them with WireMock or a stub. Quality evaluation is separate: the production model and prompt run against an evaluation set nightly and on any prompt or model change, with a baseline that blocks regressions.
Finally, model images are large, so I cache them in CI with a prebuilt image or a cached volume, and keep the container tests few.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring AI reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design a test strategy for a Spring Boot service that uses an LLM, using a local model...": https://scrivio-interview-library.netlify.app/spring-boot/llm-fundamentals/5936d72ce9/