---
title: "Mastering Testing Efficiency in Spring Boot: Optimization Strategies and Best Practices"
description: "Spring Boot test suites can be sped up substantially by loading less of the application context and reusing what is loaded."
url: "https://swe.quest/articles/mastering-testing-efficiency-in-spring-boot-optimization-strategies-and-best-44be0e347308ee5c"
canonical: "https://engineering.zalando.com/posts/2023/11/mastering-testing-efficiency-in-spring-boot-optimization-strategies-and-best-practices.html"
source: "Zalando Engineering"
published: "2023-11-13"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["testing","jvm","ci-cd","performance"]
---

# Mastering Testing Efficiency in Spring Boot: Optimization Strategies and Best Practices

A post on [Zalando Engineering](https://swe.quest/sources/zalando.md), published 2023-11-13, 9 min read, How-to. Original: [https://engineering.zalando.com/posts/2023/11/mastering-testing-efficiency-in-spring-boot-optimization-strategies-and-best-practices.html](https://engineering.zalando.com/posts/2023/11/mastering-testing-efficiency-in-spring-boot-optimization-strategies-and-best-practices.html)

## Summary

Spring Boot test suites can be sped up substantially by loading less of the application context and reusing what is loaded. Test slices (@DataJpaTest, @JsonTest, @WebMvcTest) replace full @SpringBootTest contexts, with worked fixes for common pitfalls: disabling H2 replacement via @AutoConfigureTestDatabase, importing needed beans, and using REQUIRES\_NEW transactions so multithreaded tests see committed data. Consolidating @MockBean declarations into a shared base class or @TestConfiguration keeps the context cache key stable, @DirtiesContext should be avoided, and JUnit 5 parallel execution adds further gains. Together these cut Zalando's CI test time from about 10 minutes to 4m15s, roughly 60% faster.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)
- [Java & JVM](https://swe.quest/tags/jvm.md) (Languages)
- [CI/CD](https://swe.quest/tags/ci-cd.md) (Reliability & operations)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- Spring Boot
- JUnit 5
- Spring dependency injection ([easy posts on Application security](https://swe.quest/tags/appsec.md?difficulty=easy))
- JPA transactions ([easy posts on Consistency models](https://swe.quest/tags/consistency.md?difficulty=easy))

## Keywords

Spring Boot, JUnit 5, @DataJpaTest, @WebMvcTest, @JsonTest, @MockBean, Spring TestContext, Kotlin

## Related posts

- [Functional tests with Testcontainers](https://swe.quest/articles/functional-tests-with-testcontainers-8e8bddfb01d0b1bc.md): Zalando Engineering · 2022-04-11 · Medium · 7 min read · Testing, Java & JVM, Containers
  Black-box functional tests for a Spring service can run against the service packaged as a Docker image, with PostgreSQL and other dependencies started as separate containers via Testcontainers.
- [Mobile Developer Experience at Slack](https://swe.quest/articles/mobile-developer-experience-at-slack-f2b2b79d74a881fd.md): Slack Engineering · 2022-10-12 · Medium · 14 min read · Developer productivity, Mobile, CI/CD, Testing, Build systems
  A dedicated mobile Developer Experience team grew from 3 to 8 engineers and cut CI feedback time and failure rates by targeting the pain points that surveys, NPS and metrics like time to merge showed hurt developers most. iOS test time fell from about 50 minutes to an average of 9 by moving from Xcodebuild to Bazel with disk caching and bazel-diff, so only affected targets are tested.
- [Bun v0.6.4](https://swe.quest/articles/bun-v0-6-4-48c1f86cc2294704.md): Bun · 2023-05-26 · Medium · 4 min read · JavaScript, Testing, Runtimes & VMs, Performance engineering
  Bun v0.6.4 makes \`bun test\` up to 80% faster on suites with many small files by no longer forcing a synchronous garbage collection after every file, at the cost of roughly 9% more memory.
- [The quest for the five minute deploy](https://swe.quest/articles/the-quest-for-the-five-minute-deploy-9b91763fbfd58020.md): incident.io · 2025-07-22 · Medium · 9 min read · CI/CD, Go, Build systems, Caching, Developer productivity
  incident.io cut CI/CD times by moving from a hosted provider, where a cold Go build cache made compiling 15,000+ tests across 350+ packages the bottleneck, to Buildkite running on three self-hosted 48-vCPU servers.
- [Leveraging Testcontainers for Complex Integration Testing in Mattermost Plugins](https://swe.quest/articles/leveraging-testcontainers-for-complex-integration-testing-in-mattermost-plugins-3edaea4757026529.md): Docker · 2024-10-08 · Medium · 6 min read · Testing, Containers, Go, JavaScript
  Mattermost replaced brittle mock-based plugin tests with Testcontainers-driven integration tests that spin up a Mattermost server, PostgreSQL on an internal Docker network, and optional API mock containers.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
