Vararg method call on mock object fails when used org.mockito.AdditionalAnswers#delegatesTo
See original GitHub issueI 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:
- Created 7 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 currentgetArguments()
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)
.Yes, I agree with you. But I think, this is already a bit confusing that method
getArgument()
returns expanded varargs. Maybe we need two methodsgetArguments()
andgetExpandedArguments()
?