
Spring's TestContext Context Caching is one of the most unknown hidden gems of testing with Spring Boot - it can cut your build times in half, and often even more. Yet most developers aren't aware of it, and once they discover it, they need a tool to tell them how to optimize their test suite. That's where the Spring Test Profiler comes in.
The Spring Test Profiler is a Spring Test utility that provides visualization and insights for Spring Test execution, with a focus on Spring context caching. It helps you identify optimization opportunities in your Spring Test suite to speed up your builds and ship to production faster and with more confidence.
Fast build times = fast feedback and accelerated feature delivery!
Find more information about the profiler on our website.
Overall goal: Identify optimization opportunities in your Spring Test suite to speed up your builds and ship to production faster and with more confidence 🚤
![]() |
![]() |
This profiler helps you:
spring.factories file or @TestExecutionListeners annotationThis profiler works with Java 17+ and is compatible with:
[!WARNING] This project is highly work-in-progress and should be considered a prototype to gather feedback and ideas for future development.
What's currently not working or missing:
Add the dependency to your project:
<dependency>
<groupId>digital.pragmatech.testing</groupId>
<artifactId>spring-test-profiler</artifactId>
<version>0.1.1</version>
<scope>test</scope>
</dependency>
Add the dependency to your project:
testRuntimeOnly("digital.pragmatech.testing:spring-test-profiler:0.1.1")
Pick either one of the following methods to activate the profiler in your tests.
Add a file named META-INF/spring.factories to your resources directory with the following content:
org.springframework.test.context.TestExecutionListener=\
digital.pragmatech.testing.SpringTestProfilerListener
org.springframework.context.ApplicationContextInitializer=\
digital.pragmatech.testing.diagnostic.ContextDiagnosticApplicationInitializer
Add the @TestExecutionListeners and @ContextConfiguration annotations to your test classes:
@TestExecutionListeners(
value = {SpringTestProfilerListener.class},
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
@ContextConfiguration(initializers = ContextDiagnosticApplicationInitializer.class)
This needs to be done for each test class where you want to use the profiler. Preferably, use this on a central abstract integration test class or use the automatic activation method above.
Execute your tests:
# Maven
./mvnw verify
# Gradle
./gradlew build
After test execution, find the HTML report at:
target/spring-test-profiler/latest.htmlbuild/spring-test-profiler/latest.htmlSpring Test Profiler can show richer context customizer details when your project exposes a
ContextCustomizerExtension bean. This is useful when a customizer class is the same across test
contexts, but its internal configuration is different. A common example is a WireMock
WireMockContextCustomizer: two tests can both use the same customizer class, while each test
configures different mock names, ports, files, or properties.
Create a Spring bean in your test application context, for example with @Component or a test
@Bean method:
package com.example.testing;
import digital.pragmatech.testing.extensions.ContextCustomizerExtension;
import org.springframework.stereotype.Component;
@Component
class ExampleContextCustomizerExtension implements ContextCustomizerExtension {
@Override
public boolean supports(Object contextCustomizer) {
// Return true only for the customizer type this extension knows how to describe.
return contextCustomizer.getClass().getName().contains("ExampleContextCustomizer");
}
@Override
public String describe(Object contextCustomizer) {
// Return a stable, human-readable summary of the fields that make contexts differ.
return contextCustomizer.getClass().getSimpleName() + "[configuration=custom]";
}
}
The supports(...) method should be narrow: check the exact customizer class or a known interface.
The describe(...) method should include only deterministic configuration values that help explain why Spring created
a separate context. Avoid identity hashes, timestamps, random ports, or other values that change between runs unless
they are the actual configuration you want to compare.
If no extension supports a customizer, the report falls back to the customizer class simple name.
Access a demo Spring Test Profiler report here.
Found a bug? Please help us improve by reporting it:
We welcome contributions! Here's how to get started:
pre-commit install (pre-commit download)./mvnw install
./mvnw test
feat: add new feature, fix: resolve issue #123)$ claude mcp add spring-test-profiler \
-- python -m otcore.mcp_server <graph>