Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kzander91 opened this issue Jun 15, 2021 · 1 comment
Labels
for: backport-to-4.2.x Issues that will be back-ported to the 4.2.x line in: test type: bug
Milestone

Comments

@kzander91
Copy link

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();
}
@kzander91 kzander91 added status: waiting-for-triage Issues that we did not analyse yet type: bug labels Jun 15, 2021
@fmbenhassine fmbenhassine added in: test and removed status: waiting-for-triage Issues that we did not analyse yet labels Jun 25, 2021
@fmbenhassine fmbenhassine added this to the 4.3.4 milestone Jun 25, 2021
@fmbenhassine fmbenhassine added the for: backport-to-4.2.x Issues that will be back-ported to the 4.2.x line label Sep 13, 2021
@fmbenhassine
Copy link
Contributor

Thank you for reporting this issue and for providing a minimal example! Indeed, BatchTestContextCustomizer must implement equals()/hashCode() as requested by the ContextCustomizer contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: backport-to-4.2.x Issues that will be back-ported to the 4.2.x line in: test type: bug
Projects
None yet
Development

No branches or pull requests

2 participants