class cast on a mock?

16,071 views
Skip to first unread message

Michael Meire

unread,
Jan 27, 2009, 1:33:59 PM1/27/09
to moc...@googlegroups.com
Hi mockito group,

is there a way to deal with class casts on mocked objects?

I know, first question would be if I really need the class casting.
But, yes, I do. The reason is that I am using JAX-WS (=webservice) generated classes, more exactly a generated client stub/proxy. I need to cast that proxy to the BindingProvider interface, to set some properties on it.
(Unfortunately the generated proxy interface does not extend the BindingProvider interface)

So, in my production code, I need to cast the proxy to a BindingProvider.
But, in my test, I am using a mock for the proxy.
Result: ClassCastExceptions when running the test...

Any ideas? Or maybe I miss something here?

Thanks in advance!
Michael

szczepiq

unread,
Jan 27, 2009, 1:47:28 PM1/27/09
to moc...@googlegroups.com
Hi,

Let me get this straight:

code:

BindingProvider p = (BindingProvider) someProxy;

test:

SomeProxy p = mock(SomeProxy.class);

You probably want to mock BindingProvider class, not SomeProxy class, right?

Cheers,
Szczepan Faber

Michael

unread,
Jan 28, 2009, 2:27:29 AM1/28/09
to mockito
Hi,

Actually, it is more like this

public void someMethod(SomeProxy someproxy) {
...
BindingProvider p = (BindingProvider) someProxy;
...
}

I am writing a test for someMethod, and I want to provide a mock for
SomeProxy.
So: the following options don't work
-option 1: someMethod((SomeProxy) mock(BindingProvider))
-option 2: someMethod(mock(SomeProxy))

Both give ClassCastExceptions

Changing the method signature to someMethod(BindingProvider p) also
doesn't do the trick, because that only shifts the problem

Any more ideas?

thanks!
Michael



On Jan 27, 7:47 pm, szczepiq <szcze...@gmail.com> wrote:
> Hi,
>
> Let me get this straight:
>
> code:
>
> BindingProvider p = (BindingProvider) someProxy;
>
> test:
>
> SomeProxy p = mock(SomeProxy.class);
>
> You probably want to mock BindingProvider class, not SomeProxy class, right?
>
> Cheers,
> Szczepan Faber
>

szczepiq

unread,
Jan 28, 2009, 4:38:04 AM1/28/09
to moc...@googlegroups.com
Hi,

I've got two ideas for you:

1. Shifting the problem :)

> Changing the method signature to someMethod(BindingProvider p) also
> doesn't do the trick, because that only shifts the problem

2. Creating an an adapter-style interface in your test code that
implements both SomeProxy & BindingProvider

Will that work for you?

Cheers,
Szczepan Faber

Pintail

unread,
Jan 28, 2009, 8:19:38 AM1/28/09
to moc...@googlegroups.com
Hi Szczepan and Michael,

> 2. Creating an an adapter-style interface in your test code that
> implements both SomeProxy & BindingProvider

Is there a reason why Mockito doesn't allow to create a mock
implementing multiple interfaces?
The ClassImposterizer used by Mockito in MockUtil.createMock() already
allows it.

Kind regards,

Kris Vandebroek

szczepiq

unread,
Jan 28, 2009, 8:53:02 AM1/28/09
to moc...@googlegroups.com
Hi,

>Is there a reason why Mockito doesn't allow to create a mock
>implementing multiple interfaces?

No there isn't, maybe apart from keeping the api slim :)

Wrote a feature request for it:
http://code.google.com/p/mockito/issues/detail?id=51

Cheers,
Szczepan Faber

szczepiq

unread,
Apr 21, 2009, 6:30:20 PM4/21/09
to moc...@googlegroups.com
Hi,

We almost implemented this feature but... it seems we'd like to dig up
this old thread.

The reason we hesitate about this feature is we'd prefer to keep
Mockito API slim (please, not another overloaded mock() method!).
Therefore I'm asking again how useful it is? Can someone share a code
sample with legitimate case for this feature?

We're very tempted to slip it to later release or even forget about it...

Cheers,
Szczepan Faber

lukester

unread,
Apr 13, 2011, 11:57:58 AM4/13/11
to moc...@googlegroups.com
For anyone else needing this functionality, it's worth mentioning that it has been implemented in the API, it's at the end of the feature request linked above, but to save clicking, this is it:

mock(Foo.class, withSettings().extraInterfaces(Bar.class));
As regards the rationale for needing it, I encountered a test scenario where I wouldn't have succeeded without this feature. I had Servlet code that was testing the implementation of a Filter:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain){

	HttpServletRequest hrequest = (HttpServletRequest) request;
	HttpServletResponse hresponse = (HttpServletResponse) response;
...            
              }

My test failed with a class cast exception even though I thought this would work:

ServletRequest sRequest = mock(HttpServletRequest.class);

With the new feature, it looks like this:
ServletRequest request = mock(ServletRequest.class, withSettings().extraInterfaces(HttpServletRequest.class));

Hope this helps someone.
Cheers
Message has been deleted

meiiiiii

unread,
Jun 6, 2014, 5:55:50 AM6/6/14
to moc...@googlegroups.com
Very helpful, thanks! I may add that it can be done with annotations:
    @Mock(extraInterfaces = {HttpServletRequest.class})
Reply all
Reply to author
Forward
0 new messages