Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocked generic method returns incorrect mocked type #435

ogray58 opened this issue Jul 25, 2017 · 1 comment

Mocked generic method returns incorrect mocked type #435

ogray58 opened this issue Jul 25, 2017 · 1 comment


Copy link

@ogray58 ogray58 commented Jul 25, 2017

JMockit: 1.33
JUnit: 4.11
Java: 1.8.0_66

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;

public void test()
	System.out.println(new TestClass().testMethod("test"));

private static class TestClass
	public <R> R testMethod(R value)
		return value;
@ogray58 ogray58 changed the title Mocked method returning generic returns incorrect mock class Mocked generic method returns incorrect mocked type Jul 25, 2017
@rliesenfeld rliesenfeld self-assigned this Jul 26, 2017
@rliesenfeld rliesenfeld added the bug label Jul 26, 2017

This comment has been minimized.

Copy link

@ogray58 ogray58 commented Jul 26, 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.

Zipped code to reproduce

@jmockit jmockit locked and limited conversation to collaborators Sep 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
2 participants
You can’t perform that action at this time.