views:

46

answers:

1

We're looking at switching to Spring 3.0 and running into problems with the intersection of Spring 3.0, EasyMock, and Java Generics.

In one place, we're mocking a Spring 3.0 AbstractBeanFactory, specifically this method:

public Class<?> getType(String name) throws NoSuchBeanDefinitionException { ... }

Under earlier versions of Spring, this returns a non-generic and all was well. With the generic, however, we run into trouble with this:

expect(mockBeanFactory.getType(CLASS_NAME)).andReturn(SOME_CLASS);

Because getType returns Class<?>, andReturn requires Class<?> as a parameter, which simply doesn't work properly.

Is there a known workaround to this?

+3  A: 

I've run into a problem like this before, with Mockito. I'm not sure why it happens yet. You can cast the expect(..) argument to the non-generic Class type, ala

expect((Class) mockBeanFactory.getType(CLASS_NAME)).andReturn(SOME_CLASS);

Then you'll just have a warning, which you can suppress if you want. Not a very elegant solution; I'm going to spend a few more minutes looking at it.

Ladlestein
That does the trick. Sadly, I've come to expect inelegant solutions when dealing with Java Generics.
Alan Krueger