30-seconds-of-java8
30-seconds-of-java8 copied to clipboard
Fix flaky test for pick_should_pick_key_pairs_corresponding_to_keys
This PR is to fix a flaky test snippets.SnippetsTests#pick_should_pick_key_pairs_corresponding_to_keys
setup
Java version: 1.8.0_382 Maven version: Apache Maven 3.6.3
Test failure Reproduction
The test snippets.SnippetsTests#pick_should_pick_key_pairs_corresponding_to_keys can fail as the underlying code uses a HashMap and containsExactly assumes the order of the elements in the HashMap This issue was verified using the NonDex plugin.
Steps:
Use the nondex .sh script from "Helper scripts in cmds" section
./nondex.sh . snippets.SnippetsTests#pick_should_pick_key_pairs_corresponding_to_keys
NonDex test failure:
[ERROR] Failures:
[ERROR] SnippetsTests.pick_should_pick_key_pairs_corresponding_to_keys:331
Actual and expected have the same elements but not in the same order, at index 0 actual element was:
<MapEntry[key="c", value=3]>
whereas expected element was:
<a=1>
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
Root cause and fix
containsExactly assumes the order of the elements in the HashMap, so using containsOnly can be order agnostic. But contains only broaden the concept about the exactly, so we check the size first then compare the content