liveBook · Manning

Unit test

As explained in Testing Spring Boot Applications

A test that isolates a small piece of code, such as a class or method, to verify its behavior independently.

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

How Testing Spring Boot Applications covers it

How deep it goes
Explains isolation, collaborators, assertions, and trade-offs, but stops short of full application-context or system verification.
Examples
Demonstrates a WeatherScoringService test using weather history, user preferences, mocked collaborators, a score call, and an assertion.
What you can do after
Apply it via focused JUnit tests that exercise complex service behavior without starting Spring Boot.
How it connects
Contrasts unit tests with integration tests, end-to-end tests, Spring Boot tests, and application-context testing.

Also explained in

Compare how 18 books explain unit test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.