Generics and JMock

O

Owen Jacobson

An open challenge for the newsgroup at large, while I'm thinking about
this:

I'm trying to use JMock to create a mock instance of a generic
interface (EventDispatcher<T>). This is, quite obviously, the eight-
lane-express highway to hell, since generics and mockery do not mix in
any way.

While it's correct,
EventDispatcher<Foo> fooMock = mock (EventDispatcher.class)
generates an unchecked cast warning, which is ugly. Harmless, but
ugly. The challenge is to create a type signature for mock(Class c)
that allows this to succeed without warnings without allowing
nonsensical mocks like

String mockString = mock (CharSequence.class)

..

My initial attempt was this signature, which allows both the former
and the latter:

<C, T extends C> T mock (Class<C> c)

Any thoughts?
-o
 
R

Roedy Green

My initial attempt was this signature, which allows both the former
and the latter:

I usually start to splutter with rage at trying to solve these
problems. Generics are supposed to make coding safer not more hideous
and complicated. There is something fundamentally wrong with generics
when it starts requiring these sorts of gavottes.

My usual tactic is to scan the code in src.zip for source code similar
to what I am trying to do, then puzzle why the Sun code works, then
copy the technique.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top