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.

Vararg method call on mock object fails when used org.mockito.AdditionalAnswers#delegatesTo

See original GitHub issue

I try to mock interface with varargs method and default implementation in final class and get this error:

java.lang.IllegalArgumentException: wrong number of arguments

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mockito.internal.stubbing.defaultanswers.ForwardsInvocations.answer(ForwardsInvocations.java:31)
    at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
    at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
    at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38)
    at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:51)

I run the following code:

public class DefaultAnswerDemo {

    public interface Foo {
        void bar(String baz, Object... args);
    }

    public static final class FooImpl implements Foo {

        @Override
        public void bar(String baz, Object... args) {
            System.out.println("bar");
        }

    }

    @Test
    public void defaultAnswerTest() {
        Foo fooImpl = new FooImpl();

        Foo foo = mock(Foo.class, withSettings()
                .defaultAnswer(delegatesTo(fooImpl)));

        foo.bar("baz", 12, "45", 67.8);
    }

}

This is happens because org.mockito.internal.invocation.InvocationImpl by default expands varargs arguments to flat list. And I have no idea, how correctly fix this.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ChristianSchwarzcommented, May 24, 2016

Adding new API or changing existing behaviour need to be discussed with the core-members.

I agree with you, getArguments() should return unaltered/not expanded arguments. The current getArguments() implementation has also the problem that is can’t distinguish an null-vararg argument / varArgMethod(new Type[]{null}) from an null-varag array / varArgMethod((Type[])null).

0reactions
andreymanzharovcommented, May 24, 2016

Yes, I agree with you. But I think, this is already a bit confusing that method getArgument() returns expanded varargs. Maybe we need two methods getArguments() and getExpandedArguments()?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to properly match varargs in Mockito - java - Stack Overflow
ArgumentMatcher; import org.mockito.internal.matchers. ... I had to use the any(Class type) method to match an array arg being passed as a ...
Read more >
mockito-scala
org.mockito.MockitoSugar · Fixes the compiler errors that sometimes occurred when using overloaded methods that use varargs like doReturn · Eliminates the need to ......
Read more >
Mockito 2.2.7 API
Mockito verifies argument values in natural java style: by using an equals() method. Sometimes, when extra flexibility is required then you might use...
Read more >
Mockito - DZone Refcardz
It can be done using Mockito.when() in conjunction with thenReturn () . This process of defining how a given mock method should behave...
Read more >
ArgumentCaptor (Mockito 3.4.0 API) - javadoc.io
Class ArgumentCaptor<T>. java.lang.Object. org.mockito.ArgumentCaptor<T> ... Use it when capturing varargs or when the verified method was called multiple ...
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