45

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>".

Kevin Wong
  • 14,656
  • 11
  • 42
  • 52

5 Answers5

42

AFAIK, you can't avoid the unchecked warning when a class name literal is involved, and the SuppressWarnings annotation is the only way to handle this.

Note that it is good form to narrow the scope of the SuppressWarnings annotation as much as possible. You can apply this annotation to a single local variable assignment:

public void testSomething() {

    @SuppressWarnings("unchecked")
    Foo<Integer> foo = EasyMock.createMock(Foo.class);

    // Rest of test method may still expose other warnings
}

or use a helper method:

@SuppressWarnings("unchecked")
private static <T> Foo<T> createFooMock() {
    return (Foo<T>)EasyMock.createMock(Foo.class);
}

public void testSomething() {
    Foo<String> foo = createFooMock();

    // Rest of test method may still expose other warnings
}
Lahiru Ashan
  • 767
  • 9
  • 16
Barend
  • 17,296
  • 2
  • 61
  • 80
12

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);
  • Additionally, don't forget to use the `org.easymock.classextension.EasyMock` version of EasyMock to create your mocks when using the abstract class. – Andreas Oct 17 '12 at 14:41
  • As of EasyMock 3.0 (May 2010), the classes in `org.easymock.classextension` are deprecated and are simple proxies to the same-named classes in `org.easymock`. It is recommended to remove ".classextension" from your imports and stop compiling with the classextension library. – AndrewF Jul 21 '17 at 10:20
3

The two obvious routes are to suppress the warning or mock a subclass.

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

(Disclaimer: Not even attempted to compile this code, nor have I used EasyMock.)

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • The syntax should probably be: private static interface SomeClass_Integer extends SomeClass {} I have the same problem and this is the work around I use so the approach will work. But I hope somebody has the answer we are looking for – Brian Matthews Sep 11 '08 at 16:14
2

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);
}
Cem Catikkas
  • 7,171
  • 4
  • 29
  • 33
1

I know this goes against the question, but why not create a List rather than a Mock List?

It's less code and easier to work with, for instance if you want to add items to the list.

MyItem myItem = createMock(myItem.class);
List<MyItem> myItemList = new ArrayList<MyItem>();
myItemList.add(myItem);

Instead of

MyItem myItem = createMock(myItem.class);
@SuppressWarnings("unchecked")
List<MyItem> myItemList = createMock(ArrayList.class);
expect(myItemList.get(0)).andReturn(myItem);
replay(myItemList);
chim
  • 8,407
  • 3
  • 52
  • 60