[FLINK-39876][tests] Migrate flink-streaming-java assertions to AssertJ - #28835
[FLINK-39876][tests] Migrate flink-streaming-java assertions to AssertJ#28835spuru9 wants to merge 1 commit into
Conversation
|
As per the https://flink.apache.org/how-to-contribute/code-style-and-quality-common/#7-testing |
08a6354 to
d30fea1
Compare
The contributor guide asks for JUnit 5 and AssertJ and states "Don't use Hamcrest, JUnit assertions and `assert` directive". flink-streaming-java was already on JUnit 5 (FLINK-25544), but ten test classes still reached for Hamcrest. This removes the last of them. * TypeSafeMatcher and FeatureMatcher instances that were bridged into AssertJ via HamcrestCondition.matching() become native AssertJ Conditions. * MockitoHamcrest.argThat() becomes Mockito's own ArgumentMatchers.argThat(). The Hamcrest matchers it wrapped (containsInAnyOrder, contains, hasEntry, allOf) are expressed as predicates, which also removes the casts their call sites needed. CheckpointExceptionMatcher now implements Mockito's ArgumentMatcher rather than Hamcrest's BaseMatcher. * Drops the unused EqualsResourceSpecMatcher from StreamGraphGeneratorTest. The replacements keep the semantics of the matchers they replace, which AssertJ and Mockito do not provide for free: the predicates reject null explicitly, because TypeSafeMatcher never invoked matchesSafely() on a null actual; containsInAnyOrder counts duplicates; and the map matcher checks for the expected entries rather than map equality, as hasEntry did. No production code is touched. Each migrated predicate was inverted in turn to confirm the tests using it fail. Generated-by: Claude Code (Claude Opus 5)
d30fea1 to
0efae72
Compare
|
cc: @snuyanzin @raminqaf |
| private static Collection<TimeWindow> containsInAnyOrder(TimeWindow... windows) { | ||
| return argThat( | ||
| actual -> { | ||
| if (actual == null) { | ||
| return false; | ||
| } | ||
| final List<TimeWindow> remaining = new ArrayList<>(actual); | ||
| for (TimeWindow window : windows) { | ||
| if (!remaining.remove(window)) { | ||
| return false; | ||
| } | ||
| } | ||
| return remaining.isEmpty(); | ||
| }); | ||
| } |
There was a problem hiding this comment.
It does not look like a solution
we have multiple tests using this method (hamcrest's containsInAnyOrder).
How should we continue there? Create same helper for every class instead of hamcrest?
Please do not blindly trust AI
| * Matches a collection containing exactly the expected windows, in any order, counting | ||
| * duplicates. | ||
| */ | ||
| private static Collection<TimeWindow> containsInAnyOrder(TimeWindow... windows) { |
There was a problem hiding this comment.
snuyanzin
left a comment
There was a problem hiding this comment.
given this approach https://github.com/apache/flink/pull/28835/changes#r3695518844
I would prefer existing code since no need to have code duplication for every usage of hamcrest method
|
Ok, Got it. Picked it as the wiki not discourages Hamcrest. |
What is the purpose of the change
flink-streaming-javais already on JUnit 5 (FLINK-25544), but ten test classes still usedHamcrest — either bridged into AssertJ via
HamcrestCondition.matching(...)or through theMockitoHamcrestargument matchers. This removes the last of them, per§7 Testing:
"Don't use Hamcrest, JUnit assertions and
assertdirective". Test-only; no production codeis touched.
Brief change log
TypeSafeMatcher/FeatureMatcherwrapped inHamcrestCondition.matching(...)become nativeAssertJ
Conditions.MockitoHamcrest.argThat(...)becomesArgumentMatchers.argThat(...), with the Hamcrestmatchers it wrapped expressed as predicates;
CheckpointExceptionMatchernow implementsMockito's
ArgumentMatcherinstead of Hamcrest'sBaseMatcher.EqualsResourceSpecMatcherfromStreamGraphGeneratorTest.Verifying this change
This change is a trivial rework / code cleanup without any test coverage.
Covered by the tests it touches (99 tests, passing). To confirm no assertion became vacuously
true, each new predicate was inverted in turn and the tests using it failed as expected.
Checkstyle and Spotless are clean.
Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Claude Opus 5)