greyblue9 / dexmaker

Automatically exported from code.google.com/p/dexmaker
1 stars 0 forks source link

IllegalArgumentException for no-argument method mocking with Mockito #13

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Stubbing a call of method without arguments with Mockito 1.9.5-rc1 fails with 
IllegalArgumentException. I'm using robolectric-1.1 to run the unit test 
locally.

Here's code I'm using to verify this:

interface Foo {
    String noArgs();
}
@Test
public void testNoArgMethod() {
    Foo mockedObject = mock(Foo.class);
    when(mockedObject.toString()).thenReturn("foobar");
    assertEquals(mockedObject.toString(), "foobar");
}

Test is in error state due to:
java.lang.IllegalArgumentException
    at com.google.dexmaker.mockito.InvocationHandlerAdapter.invoke(InvocationHandlerAdapter.java:49)
    at com.seven.opencare.data.$Proxy12.toString(Unknown Source)
    at NoArgMethodTest.testNoArgMethod(NoArgMethodTest.java:15)
    ...

According to Mockito's documentation, it is expected to get 
InvocationHandler.invoke where args==null if the method being called does not 
have arguments. It seems safe to remove the null check throwing the exception 
from InvocationHandlerAdapter.invoke() implementation, as according to 
documentation it actually isn't illegal argument.

Original issue reported on code.google.com by mikko.li...@gmail.com on 22 Aug 2012 at 5:37

GoogleCodeExporter commented 8 years ago
Fixed in the latest Mockito.

Original comment by limpbizkit on 10 Oct 2012 at 3:45

GoogleCodeExporter commented 8 years ago
Why this impl. checks for args==null if InvocationHandler  doesn't specifies 
this contract ?

If dexmaker is used in conjunction with mockito to run unit tests as JUnit 
tests then an IllegalArgumentException is thrown for no args methods. 

Original comment by vadim.ba...@gmail.com on 24 Oct 2012 at 9:07