question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot verify varargs parameter as an array

See original GitHub issue

Cannot verify the varargs by comparing with an array.

    @Test
    public void shouldVerifyVarargsAsArray() throws Exception {
        IMethods mock = mock(IMethods.class);

        mock.mixedVarargs("1", "2", "3");

        verify(mock).mixedVarargs(any(), eq(new String[] {"2", "3"}));
    }

Expected: test runs successfully Actual: the test fails with an argument mismatch error.

org.mockitousage.verification.BasicVerificationTest > shouldVerifyVarargsAsArray FAILED
    Argument(s) are different! Wanted:
    iMethods.mixedVarargs(<any>, ["2", "3"]);
    -> at org.mockitousage.verification.BasicVerificationTest.shouldVerifyVarargsAsArray(BasicVerificationTest.java:125)
    Actual invocation has different arguments:
    iMethods.mixedVarargs("1", "2", "3");
    -> at org.mockitousage.verification.BasicVerificationTest.shouldVerifyVarargsAsArray(BasicVerificationTest.java:123)
    ...

This was found while upgrading from 1.9.5. In that version it was possible to achieve something similar by creating a custom ArgumentMatcher that implemented VarargMatcher. In that case the ArgumentMatcher was given the varargs as an array. However, in the latest version the same matcher is passed each vararg separately which prevents it from matching.

It is not possible to inline the array as follows because in the actual code the contents of the array are passed as an array into the method that does the verification. verify(mock).mixedVarargs(any(), eq("1"), eq("2"));

I tried doing something like this:

    verify(mock).mixedVarargs(any(), inline(new String[] {"1", "2"}));
  }
  public static <T> T inline(T[] array) {
    for (Object o : array) {
      eq(o);
    }
    return null;
  }

But that failed in MatchersBinder because the number of matchers provided (3) did not match the number of arguments passed in the invocation (2).

I also tried to remove the VarargMatcher from the custom ArgumentMatcher but that failed too as the number of arguments did not match in MatcherApplicationStrategy.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
alycecilcommented, Feb 9, 2022

This is still an issue.

0reactions
alycecilcommented, Feb 9, 2022

It looks like https://github.com/mockito/mockito/pull/1461/files still fixes it, and can just be included as needed in test code. But it would be a nice to have.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to properly match varargs in Mockito - java - Stack Overflow
I had to use the any(Class type) method to match an array arg being passed as a varargs parameter. ArgumentMatchers.any(Class type).
Read more >
Fixing ugly Java APIs: read-only generic varargs
This is forbidden because Java arrays are covariant and they check the type of values written to them at runtime. But at runtime,...
Read more >
Arrays should not be created for varargs parameters
argument ; varargs is an array. Simply pass the elements directly. They will be consolidated into an array automatically. Incidentally passing an array...
Read more >
Java Varargs (Variable Arguments) With Examples - Programiz
The ... syntax tells the Java compiler that the method can be called with zero or more arguments. As a result, nums variable...
Read more >
Non-Reifiable Types - The Java™ Tutorials
When the compiler encounters a varargs method, it translates the varargs formal parameter into an array. However, the Java programming language does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found