Skip to content

[fix][test] Fix MultiBrokerLeaderElectionExpirationTest Mockito spy usage#25425

Merged
lhotari merged 1 commit intoapache:masterfrom
merlimat:fix/flaky-MultiBrokerLeaderElectionExpirationTest
Mar 30, 2026
Merged

[fix][test] Fix MultiBrokerLeaderElectionExpirationTest Mockito spy usage#25425
lhotari merged 1 commit intoapache:masterfrom
merlimat:fix/flaky-MultiBrokerLeaderElectionExpirationTest

Conversation

@merlimat
Copy link
Copy Markdown
Contributor

Motivation

Fix compilation/runtime issue in MultiBrokerLeaderElectionExpirationTest.

The test uses when(spy.getDefaultMetadataCacheConfig()).thenReturn(...) which doesn't work correctly with Mockito spies on concrete classes — it actually invokes the real method during stubbing setup. Also, the MetadataCacheConfig.builder().build() returns a raw type that needs proper generic casting.

Modifications

  • Changed when(spy).thenReturn() to doReturn().when(spy) pattern for correct Mockito spy behavior
  • Added proper @SuppressWarnings("unchecked") cast for MetadataCacheConfig<Object>
  • Added missing import static org.mockito.Mockito.doReturn

Documentation

  • doc-not-needed

Matching PR in forked repository

No response

@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Mar 28, 2026
Fix Mockito spy compatibility issue: when(spy).thenReturn() doesn't work
correctly with Mockito spies on concrete classes. Use doReturn().when(spy)
pattern instead. Also add proper generic cast with @SuppressWarnings for
the MetadataCacheConfig builder.
@merlimat merlimat force-pushed the fix/flaky-MultiBrokerLeaderElectionExpirationTest branch from 9c27977 to 5e3be4c Compare March 28, 2026 17:54
@lhotari lhotari merged commit 1ee18b4 into apache:master Mar 30, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/test doc-not-needed Your PR changes do not impact docs ready-to-test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants