How to mock an array?

21,844 views
Skip to first unread message

Danny

unread,
Dec 2, 2010, 2:24:52 PM12/2/10
to mockito
I am trying to mock a simple Date array using

Date[] s = mock(Date[].class);

but I get the following exception

java.lang.IllegalArgumentException: Cannot subclass final class class
[Ljava.util.Date;
at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)
at
org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:
25)
at
org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:
217)
at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
at
org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:
93)
at
org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:
50)
at
org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:
100)
at
org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:
54)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138)


Date class is not final yet I get this error. Can anybody help me
figure out how to mock an array? I am using mockito 1.8.5.

Thanks.
Danny

Felix Leipold

unread,
Dec 2, 2010, 3:02:53 PM12/2/10
to moc...@googlegroups.com
Hi Danny,

The class that it's complaining about is [Ljava.util.Date, which is the reified type of an array of Dates. The point is that arrays are final. Also, why would you want to mock an array? 
It is usually quite simple to create an array.

Cheers,
Felix 


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


Geoffrey Wiseman

unread,
Dec 2, 2010, 3:12:57 PM12/2/10
to moc...@googlegroups.com
On Thu, Dec 2, 2010 at 3:02 PM, Felix Leipold <felix....@gmail.com> wrote:
Hi Danny,

The class that it's complaining about is [Ljava.util.Date, which is the reified type of an array of Dates. The point is that arrays are final. Also, why would you want to mock an array? 
It is usually quite simple to create an array.

One wouldn't normally mock an array, agreed.  

Mocking is usually accomplished by introducing a dynamic proxy on an interface or a subclass of a class.  Since a date array is a final class, it is neither an interface nor subclassable, so that takes away the normal approaches for creating a mock.
  
  - Geoffrey
--
Geoffrey Wiseman
http://www.geoffreywiseman.ca/

Danny

unread,
Dec 2, 2010, 3:27:50 PM12/2/10
to mockito
Felix,

Thanks for your response. I see your point about the final class. The
reason why I am trying to mock an array is because I am working with a
spy object so when I call a method using
when(spyObject.someRealMethod).thenReturn(someArray), the real method
is called. I do not want to call the real method. I simply want to
mock it to return an array, so I am using PowerMockito's doReturn
expression for example -

doReturn(someArray).when(spyObject.someRealMethod).

If I try to give a real array (not a mock one) in the above call, I
get the following exception

org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at
org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:
31)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. although stubbed methods may return mocks, you cannot inline mock
creation (mock()) call inside a thenReturn method (see issue 53)

So I am not able to use a real array and I am unable to construct a
mock array. Do you have any idea how I should handle this?

Thanks.
Danny

On Dec 2, 2:02 pm, Felix Leipold <felix.leip...@gmail.com> wrote:
> Hi Danny,
>
> The class that it's complaining about is [Ljava.util.Date, which is the
> reified type of an array of Dates. The point is that arrays are final. Also,
> why would you want to mock an array?
> It is usually quite simple to create an array.
>
> Cheers,
> Felix
>
> > mockito+u...@googlegroups.com<mockito%2Bunsu...@googlegroups.com>
> > .

Karl Schaefer

unread,
Dec 2, 2010, 3:43:55 PM12/2/10
to moc...@googlegroups.com
Can you refactor the code to use a List?

> To unsubscribe from this group, send email to mockito+u...@googlegroups.com.

Danny

unread,
Dec 2, 2010, 3:57:43 PM12/2/10
to mockito
I am working with legacy code (which is why I am having to use spy
objects). I am not allowed to modify the code. So the method needs to
return array. I tried using when..then instead of doReturn..when but
then I get

org.mockito.exceptions.misusing.WrongTypeOfReturnValue

complaining that someRealMethod must return a list instead of array. I
don't know why it would complain about that. There's no list in the
picture and the method signature clearly returns an array and not
list.

Danny

On Dec 2, 2:43 pm, Karl Schaefer <karlgschae...@gmail.com> wrote:
> Can you refactor the code to use a List?
>

Brice Dutheil

unread,
Dec 2, 2010, 4:33:29 PM12/2/10
to moc...@googlegroups.com
Hi Danny

You are actually misusing Mockito or PowerMockito.

You wrote this, which is actually incorrect and wy you got a *UnfinishedStubbingException* :

doReturn(someArray).when(spyObject.someRealMethod());

This valid syntax is (note the parenthesis position) :

doReturn(someArray).when(spyObject).someRealMethod();





The thing is if you stub first the answer (you begin with the answer)

do*
will*

you will write the when "clause" with just the spy reference, then call the method you want to stub.
For exemple, again note the parenthesis position :

doThrow(new Exception()).when(spyRef).theStubbedMethod();



Arrays are special types in Java, and they are not mockable, at all.



Hope that helps,

-- 
Bryce


On Thu, Dec 2, 2010 at 21:27, Danny <danny.e...@gmail.com> wrote:
doReturn(someArray).when(spyObject.someRealMethod)

Brice Dutheil

unread,
Dec 2, 2010, 4:39:43 PM12/2/10
to moc...@googlegroups.com
Oh and Danny

By the way, when using spys, you should always use the

do*().when(spy).method();
will*().given(spy).method();


Because by default spys, call real methods, calling first the stubbing do* or will* will allow mockito to tell the mock that a method will be stubbed.


This behaviour does not exists with mocks as they never call really method by default.


-- 
Bryce

Danny

unread,
Dec 2, 2010, 5:48:33 PM12/2/10
to mockito
Thank you Brice. What you mentioned did seem to be the problem. I was
stubbing it wrong. I changed it to the way you recommended and now the
exceptions have gone away (I still haven't gotten my test to pass but
that's because of something else). I guess this was a newbie error on
my part.

Thanks again!

Danny

On Dec 2, 3:39 pm, Brice Dutheil <brice.duth...@gmail.com> wrote:
> Oh and Danny
>
> By the way, when using spys, you should always use the
>
> do*().when(spy).method();
> will*().given(spy).method();
>
> Because by default spys, call real methods, calling first the stubbing do*
> or will* will allow mockito to tell the mock that a method will be stubbed.
>
> This behaviour does not exists with mocks as they never call really method
> by default.
>
> --
> Bryce
>
> On Thu, Dec 2, 2010 at 22:33, Brice Dutheil <brice.duth...@gmail.com> wrote:
> > Hi Danny
>
> > You are actually misusing Mockito or PowerMockito.
>
> > You wrote this, which is actually incorrect and wy you got a
> > *UnfinishedStubbingException* :
>
> > doReturn(someArray).when(spyObject.someRealMethod());
>
> > This valid syntax is (note the parenthesis position) :
>
> > doReturn(someArray).when(spyObject).someRealMethod();
>
> > The thing is if you stub first the answer (you begin with the answer)
>
> > do*
> > will*
>
> > you will write the when "clause" with just the spy reference, then call the
> > method you want to stub.
> > For exemple, again note the parenthesis position :
>
> > doThrow(new Exception()).when(spyRef).theStubbedMethod();
>
> > Arrays are special types in Java, and they are not mockable, at all.
>
> > Hope that helps,
>
> > --
> > Bryce
>
> > On Thu, Dec 2, 2010 at 21:27, Danny <danny.explo...@gmail.com> wrote:
>
> >> doReturn(someArray).when(spyObject.someRealMethod)

Brice Dutheil

unread,
Dec 3, 2010, 4:10:26 AM12/3/10
to moc...@googlegroups.com
You are welcome.

--
Brice

> --
> You received this message because you are subscribed to the Google Groups "mockito" group.
> To post to this group, send email to moc...@googlegroups.com.

> To unsubscribe from this group, send email to mockito+u...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages