A unit test isolates a small piece of code, such as a class or method, and verifies its behavior independently. Unit tests are deterministic, specific, and extremely fast, but their narrow scope cannot establish that the complete system works. section 1.2
Use unit tests for complex logic with many variables rather than starting the full Spring Boot application. Keeping the code under test decoupled makes scenarios easier to reason about, duplicate, and cover without constructing the application’s complete dependency tree. section 2.7 section 2.8
A practical unit-test boundary is to:
- Test one focused service or domain behavior, such as calculating a weather score from history and user preferences. section 2.7
- Stub external collaborators instead of loading Spring, databases, or the complete application context. section 2.7
- Use integration tests separately when verifying that multiple units work together with Spring capabilities such as web or data layers. section 1.2
Focused scoring test
Construct test data and preferences, provide mocked collaborators to WeatherScoringService, call score("Paris", 6), and assert that the result equals 0. The test exercises one scoring behavior without running Spring Boot. section 2.7
Listing 2.9 A sample domain test without Spring-specific testing tools.
@Test
void temperatureHistoryAboveAcceptableRange() {
var data = WeatherHistoryData
.withDataPoint(2022, 6, TemperatureRange.min(12).max(32))
.withDataPoint(2023, 6, TemperatureRange.min(9).max(24))
.withDataPoint(2024, 6, TemperatureRange.min(10).max(23));
var userPrefs = UserPreferences
.acceptableRange(5, 30)
.preferredRange(15, 25);
// Omitted: configure mocks to return data above
var weatherService = new WeatherScoringService( #1
mockWeatherService, #1
mockUserPreferenceRepository #1
); #1
var score = weatherScoringService.score("Paris", 6); #2
assertThat(score).isEqualTo(0);
}
Browse the complete domain-test listing, including test data, user preferences, mocked dependencies, service construction, invocation, and assertion. section 2.7

















