Mocking multiple Interfaces in one mock?

3,534 views
Skip to first unread message

Malte Finsterwalder

unread,
Sep 23, 2009, 5:24:12 AM9/23/09
to moc...@googlegroups.com
Hi,

Is it possible to create a single mock object, that mocks multiple interfaces?

mock(First.class, Second.class) is obviously not supported.

Greetings,
Malte

Patricio Arvizu

unread,
Sep 24, 2009, 6:58:42 PM9/24/09
to mockito
You can use MockSettings.extraInterfaces(java.lang.Class<?>...
interfaces)

http://mockito.googlecode.com/svn/branches/1.8.0/javadoc/org/mockito/MockSettings.html

On 23 sep, 04:24, Malte Finsterwalder <ma...@finsterwalder.name>
wrote:

Malte Finsterwalder

unread,
Oct 1, 2009, 4:15:21 AM10/1/09
to moc...@googlegroups.com
2009/9/23 Malte Finsterwalder <ma...@finsterwalder.name>:

> Is it possible to create a single mock object, that mocks multiple interfaces?
>
> mock(First.class, Second.class) is obviously not supported.

I found a very simple solution: Create a new interface that extends
all the interfaces to mock:

public interface MockInterface extends First, Second {}
mock(MockInterface.class)

Greetings,
Malte

Graham Allan

unread,
Oct 1, 2009, 6:33:05 AM10/1/09
to moc...@googlegroups.com
That's one of the alternatives discussed here:
http://code.google.com/p/mockito/issues/detail?id=51

Summary from that is your suggestion will work, but some find it slightly ugly
and would prefer to use a method. Added advantage with the method call is
consistency.

Regards,
Graham

Malte Finsterwalder

unread,
Oct 1, 2009, 6:56:59 AM10/1/09
to moc...@googlegroups.com
Doesn't the multiple Interface mocking idea have a more fundamental problem:

m = mock(First.class, Second.class)

What type does variable m have?

Greetings,
Malte


2009/10/1 Graham Allan <grundl...@googlemail.com>:

Joe Kearney

unread,
Oct 1, 2009, 7:05:20 AM10/1/09
to moc...@googlegroups.com
The following declaration let's this work:
<T> T mock(Class<? extends T> ... classes) { ... }

This will allow the following junk to compile, but that doesn't matter.
Object mock = mock(String.class, Number.class, Comparable.class);

So, roughly speaking, your m will have type capture-of<? extends First & Second>. This is well defined only where at most one of the classes array is a class, obviously.

Joe

2009/10/1 Malte Finsterwalder <ma...@finsterwalder.name>

Malte Finsterwalder

unread,
Oct 1, 2009, 8:29:09 AM10/1/09
to moc...@googlegroups.com
Joe Kearney wrote:
> The following declaration let's this work:
>
> <T> T mock(Class<? extends T> ... classes) { ... }
>
>
> This will allow the following junk to compile, but that doesn't matter.
>
> Object mock = mock(String.class, Number.class, Comparable.class);
>
>
> So, roughly speaking, your m will have type capture-of<? extends First &
> Second>. This is well defined only where at most one of the classes
> array is a class, obviously.

Can I actually define this as the type of my variable?
If not, the creation of a mock for several interfaces doesn't help,
because I can't access it other than Object.

Greetings,
Malte

szczepiq

unread,
Oct 1, 2009, 8:30:15 AM10/1/09
to moc...@googlegroups.com
>Doesn't the multiple Interface mocking idea have a more fundamental problem:
>m = mock(First.class, Second.class)

The first class / interface wins. This is how you use it:

Foo foo = mock(Foo.class, withSettings().extraInterfaces(Bar.class, Baz.class));

Obviously Bar and Baz must be interfaces. Foo can be a class or interface.

Before using this feature make sure your code is clean & simple. In
general you should not be needing this... maybe the code should be
refactored so it is easy & simple to test?

Cheers,
Szczepan Faber

Joe Kearney

unread,
Oct 1, 2009, 8:42:35 AM10/1/09
to moc...@googlegroups.com
Oh I see, fair point. No, you'd just get Object. You can't even define methods to perform this for fixed numbers of arguments:
<T1, T2, T3, Ret extends T1 & T2 & T3> Ret mock(Class<T1> t1, Class<T2> t2, Class<T3> t3) { ... }
since you can't specify additional bounds when T1 is a type parameter.

I take it back, this probably isn't as useful as I'd hoped.

Thanks,
Joe

2009/10/1 Malte Finsterwalder <ma...@finsterwalder.name>

szczepiq

unread,
Oct 17, 2009, 6:44:01 AM10/17/09
to moc...@googlegroups.com
Hi,

(sorry for late answer but this email was marked as spam)

Yes you can do it with 1.8.1, check out docs for overloaded mock() method.

Cheers,
Szczepan
Reply all
Reply to author
Forward
0 new messages