<p>The code</p>

<pre><code>private SomeClass&lt;Integer&gt; someClass;
someClass = EasyMock.createMock(SomeClass.class);
</code></pre>

<p>gives me a warning "Type safety: The expression of type SomeClass needs unchecked conversion to conform to SomeClass&lt;Integer&gt;".</p>

[{'answer_id': 56996, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 2, 'selected': False, 'text': '<p>The two obvious routes are to suppress the warning or mock a subclass.</p>\n\n<pre><code>private static class SomeClass_Integer extends SomeClass&lt;Integer&gt;();\nprivate SomeClass&lt;Integer&gt; someClass;\n...\n    someClass = EasyMock.createMock(SomeClass_Integer.class);\n</code></pre>\n\n<p>(Disclaimer: Not even attempted to compile this code, nor have I used EasyMock.)</p>\n'}, {'answer_id': 57247, 'author': 'Cem Catikkas', 'author_id': 3087, 'author_profile': 'https://Stackoverflow.com/users/3087', 'pm_score': 2, 'selected': False, 'text': '<p>You can annotate the test method with <code>@SuppressWarnings("unchecked")</code>. I agree this is some what of a hack but in my opinion it\'s acceptable on test code. </p>\n\n<pre><code>@Test\n@SuppressWarnings("unchecked")\npublic void someTest() {\n    SomeClass&lt;Integer&gt; someClass = EasyMock.createMock(SomeClass.class);\n}\n</code></pre>\n'}, {'answer_id': 396122, 'author': 'Barend', 'author_id': 49489, 'author_profile': 'https://Stackoverflow.com/users/49489', 'pm_score': 5, 'selected': False, 'text': '<p>AFAIK, you can\'t avoid the unchecked warning when a class name literal is involved, and the <code>SuppressWarnings</code> annotation is the only way to handle this.</p>\n\n<p>Note that it is good form to narrow the scope of the <code>SuppressWarnings</code> annotation as much as possible. You can apply this annotation to a single local variable assignment:</p>\n\n<pre><code>public void testSomething() {\n\n    @SuppressWarnings("unchecked")\n    Foo&lt;Integer&gt; foo = EasyMock.createMock(Foo.class);\n\n    // Rest of test method may still expose other warnings\n}\n</code></pre>\n\n<p>or use a helper method:</p>\n\n<pre><code>@SuppressWarnings("unchecked")\nprivate static &lt;T&gt; Foo&lt;T&gt; createFooMock() {\n    return (Foo&lt;T&gt;)EasyMock.createMock(Foo.class);\n}\n\npublic void testSomething() {\n    Foo&lt;String&gt; foo = createFooMock();\n\n    // Rest of test method may still expose other warnings\n}\n</code></pre>\n'}, {'answer_id': 8897152, 'author': 'Barry John Williams', 'author_id': 1154244, 'author_profile': 'https://Stackoverflow.com/users/1154244', 'pm_score': 4, 'selected': False, 'text': '<p>I worked around this problem by introducing a subclass, e.g.</p>\n\n<pre><code>private abstract class MySpecialString implements MySpecial&lt;String&gt;{};\n</code></pre>\n\n<p>Then create a mock of that abstract class:</p>\n\n<pre><code>MySpecial&lt;String&gt; myMock = createControl().createMock(MySpecialString.class);\n</code></pre>\n'}, {'answer_id': 20427511, 'author': 'chim', 'author_id': 673282, 'author_profile': 'https://Stackoverflow.com/users/673282', 'pm_score': 1, 'selected': False, 'text': '<p>I know this goes against the question, but why not create a List rather than a Mock List?</p>\n\n<p>It\'s less code and easier to work with, for instance if you want to add items to the list.</p>\n\n<pre><code>MyItem myItem = createMock(myItem.class);\nList&lt;MyItem&gt; myItemList = new ArrayList&lt;MyItem&gt;();\nmyItemList.add(myItem);\n</code></pre>\n\n<p>Instead of </p>\n\n<pre><code>MyItem myItem = createMock(myItem.class);\n@SuppressWarnings("unchecked")\nList&lt;MyItem&gt; myItemList = createMock(ArrayList.class);\nexpect(myItemList.get(0)).andReturn(myItem);\nreplay(myItemList);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/56954', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4792/']