Skip to content

BatchTestContextCustomizer does not implement hashCode/equals, preventing context caching #3940

Closed
@kzander91

Description

@kzander91

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

  1. Unzip
  2. Run ./mvnw test
  3. 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();
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions