Description
Bug description
When using @SpringBatchTest
, the BatchTestContextCustomizer
is added to each test's MergedContextConfiguration
.
BatchTestContextCustomizer
does not implement hashCode()/equals()
, causing each new instance to have a different hash code. This leads to unnecessary cache misses and context creations, slowing down test execution.
The ContextCustomizer
Javadoc clearly demands implementing these methods.
Environment
Spring Batch 4.3.2
Steps to reproduce
Create multiple test classes with identical context configuration, annotate them with @SpringBatchTest
.
A new application context will be created for each test class.
Expected behavior
BatchTestContextCustomizer
should return stable hash codes to not break context caching.
Minimal Complete Reproducible example
demo.zip
- Unzip
- Run
./mvnw test
- The first test succeeds (ok), the second test fails (nok).
The project contains two test classes, both extending BaseTest
and thus its configuration. BaseTest
is annotated with @SpringBatchTest
and @SpringBootTest
.
The application context is injected, and @BeforeEach
records its start date in a static set.
The test method asserts that the set of context start dates contains only one value.
Removing @SpringBatchTest
fixes the test, because the same application context instance is injected into both tests.
Possible Fix
Return stable hash codes in the same way for example ExcludeFilterContextCustomizer
does it:
@Override
public boolean equals(Object obj) {
return obj != null && getClass() == obj.getClass();
}
@Override
public int hashCode() {
return getClass().hashCode();
}