Keys, configuration, and testing with a fake model
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
keep secrets safe and tests deterministic.
Planned topics
- API keys come from environment variables, never from
application.ymlcommitted to Git. - Reference the key as
${AI_API_KEY}in configuration. - Define an interface (
ProductDescriptionAssistant); the Spring AI class implements it. - In tests use a fake implementation that returns a fixed record. No network, no cost, same result every run.
- Test the invalid-output path with a fake that returns bad data.
Draft outline details
Demonstration
interface ProductDescriptionAssistant { Description describe(ProductInfo info); }
class FakeAssistant implements ProductDescriptionAssistant {
public Description describe(ProductInfo i) { return new Description("Test", "Fixed text"); }
}Check your understanding
Why a fake in tests?
Show answer
Fast, free, offline, and deterministic.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.