Unit Testing
Write effective unit tests for Spring Boot services using JUnit 5, Mockito, and AssertJ — fast, isolated tests that don't start a Spring context.
Last reviewed: January 2025
🔍 What Is It?
Unit testing in Spring Boot means testing individual classes in isolation — no Spring context, no database, no HTTP.
Key libraries (all in spring-boot-starter-test):
- →JUnit 5 — test framework (@Test, @BeforeEach, @ParameterizedTest)
- →Mockito — mock framework (@Mock, @InjectMocks, when/verify)
- →AssertJ — fluent assertions (assertThat(result).isEqualTo(expected))
Unit test characteristics:
- →Tests one class (the "unit")
- →All dependencies are mocked
- →No Spring context, no database
- →Runs in < 100ms
- →Tests behavior, not implementation
🎯 Why Do We Need It?
Unit tests are the foundation of a test suite:
- →Fast — run in milliseconds, not seconds
- →Isolated — failures point directly to the broken class
- →Cheap — no infrastructure needed
- →Design feedback — hard-to-test code indicates design problems
Constructor injection makes Spring services naturally unit-testable — just pass mocks to the constructor.
🏗️ What Happens in Real Projects?
Service layer unit tests are the most valuable:
- →Test business logic with various inputs
- →Test error conditions and exception throwing
- →Test interactions with repositories (verify correct methods called)
- →Test edge cases that are hard to reproduce with real data
⚙️ How It Works
Mockito creates proxy objects that implement interfaces or extend classes. When you call a method on a mock, it returns null/0/false by default. Use when(mock.method()).thenReturn(value) to configure behavior. Use verify(mock).method() to assert the method was called.
💻 Example
Unit Testing Spring Services — Testing service logic with Mockito mocks.
🤔 Common Confusions
❓ @InjectMocks vs @Autowired — which to use in tests?
@InjectMocks is Mockito's annotation — it creates the class under test and injects @Mock fields into it. Use it for unit tests (no Spring context). @Autowired is Spring's annotation — use it in integration tests where Spring creates and wires beans. Never use @Autowired in pure unit tests.
❓ When should I use @Mock vs @MockBean?
@Mock is Mockito — creates a mock without Spring. Use in unit tests with @ExtendWith(MockitoExtension.class). @MockBean is Spring Boot — creates a mock AND registers it in the Spring ApplicationContext. Use in integration tests with @SpringBootTest or @WebMvcTest. @MockBean is much slower because it requires a Spring context.
🚨 Common Production Issues
Tests using @SpringBootTest when they should be unit tests — 10x slower startup for no benefit
Tests not isolated — shared state between tests causing flaky failures
Mocking too much — if you mock everything, you're not testing anything
Not testing error cases — happy path only leads to production surprises
✅ Best Practices
Use @ExtendWith(MockitoExtension.class) for unit tests — no Spring context
Follow Given-When-Then structure for readable tests
Test one behavior per test method
Test error cases as thoroughly as happy paths
Use @ParameterizedTest for testing multiple inputs
Aim for 80%+ coverage on the service layer
🏛️ Architect's Note
The testing pyramid: many unit tests (fast, cheap), fewer integration tests (slower, more realistic), few E2E tests (slowest, most realistic). Unit tests give you fast feedback during development. If your unit tests are slow, you're probably starting a Spring context — that's an integration test, not a unit test. Keep unit tests pure: no Spring, no database, no network.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Spring Boot Technology Guide
Return to the Spring Boot learning hub.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Spring Boot learning path
Browse the full Spring Boot topic collection and learning order on SplashRide.
Spring Boot Interview Prep
Practice Spring Boot interview questions and scenario-based answers.
Backend Engineer Career Path
A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Integration Testing
Write Spring Boot integration tests with @SpringBootTest, Testcontainers, and @DataJpaTest — testing real component interactions with actual databases.
MockMvc
Test Spring MVC controllers with MockMvc — the fluent API for testing HTTP request handling, response status, JSON content, and security without starting a real server.