Mockito matcher and array of primitives
Stubbing with Mockito and an array of primitives? No problem! Use any()
in conjunction with AdditionalMatchers.aryEq()
. Here's a practical example:
Note: Stick with aryEq()
for efficient and hassle-free array matching that compares values, not references.
How to Tackle Any Array
For those days when the specific contents of the array don't matter, any()
comes to the rescue:
When Precision is Key - Array Verification
When an array of exact values is the game, aryEq()
is the name:
Crafting Custom Matchers
Custom matchers aren't completely forbidden, but they can get complex and tedious for primitive arrays. Keep it simple where possible with good old aryEq()
, and remember the order of verify
calls doesn't affect your test outcome:
Stubbing Deep With Complex Matchers
Deep stubbing is when things get intense. Matchers.refEq
could be an alternative to aryEq()
, particularly when dealing with arrays hiding within nested objects:
When Arrays Get Complex
Array handling could get dicey with default values, large arrays, or complex data structures. Use ArgumentCaptor or custom argument matchers in these situations to tailor the behavior and ensure smooth performance of tests.
Was this article helpful?