You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
A mocked generic method will return an instance of the mocked class, instead of a mock matching the type of the parameter. This can be demonstrated with the code below, which produces the following error when run: java.lang.ClassCastException: package.Class$TestClass cannot be cast to java.lang.String
Code to Reproduce
@Mocked private TestClass mockedTestClass = null;
@Test
public void test()
{
System.out.println(new TestClass().testMethod("test"));
}
private static class TestClass
{
public <R> R testMethod(R value)
{
return value;
}
}
The text was updated successfully, but these errors were encountered:
ogray58
changed the title
Mocked method returning generic returns incorrect mock class
Mocked generic method returns incorrect mocked type
Jul 25, 2017
I also have this case (ATest.failingTest() in the zip below) which fails under the same versions and I can't seem to condense any further. It does involve a generic method so I'm wondering if it has the same cause as this bug, but in this case the generic method does seem to return the correct type, but the test fails later due to an NPE which happens immediately after a null check on the returned object.
EDIT: Condensed the example case somewhat. Does seem related to this generics issue as removing a generic parameter from a method does make it pass. The issue does seem to manifest itself slightly differently than the case described above though.
Version
JMockit: 1.33
JUnit: 4.11
Java: 1.8.0_66
Description
A mocked generic method will return an instance of the mocked class, instead of a mock matching the type of the parameter. This can be demonstrated with the code below, which produces the following error when run:
java.lang.ClassCastException: package.Class$TestClass cannot be cast to java.lang.String
Code to Reproduce
The text was updated successfully, but these errors were encountered: