[Java] EasyMock: How do I create a mock of a genericized class without a warning?


Answers

I worked around this problem by introducing a subclass, e.g.

private abstract class MySpecialString implements MySpecial<String>{};

Then create a mock of that abstract class:

MySpecial<String> myMock = createControl().createMock(MySpecialString.class);
Question

The code

private SomeClass<Integer> someClass;
someClass = EasyMock.createMock(SomeClass.class);

gives me a warning "Type safety: The expression of type SomeClass needs unchecked conversion to conform to SomeClass<Integer>".




You can annotate the test method with @SuppressWarnings("unchecked"). I agree this is some what of a hack but in my opinion it's acceptable on test code.

@Test
@SuppressWarnings("unchecked")
public void someTest() {
    SomeClass<Integer> someClass = EasyMock.createMock(SomeClass.class);
}



Links