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

ClassCastException when combining InjectMocks with generified Mock field #2958

Closed
jsteenbeeke opened this issue Mar 31, 2023 · 4 comments · Fixed by #2962
Closed

ClassCastException when combining InjectMocks with generified Mock field #2958

jsteenbeeke opened this issue Mar 31, 2023 · 4 comments · Fixed by #2962

Comments

@jsteenbeeke
Copy link

When calling MockitoAnnotations.openMocks(this); on a class that contains an @InjectMocks target, a ClassCastException occurs when the target has a field that is generified and provided by one of the mocks in said class.

public class OpenMocksTest
{
	@InjectMocks
	protected ConcreteJob job;

	@Mock
	JobInstance<ConcreteJob> instance;

	@BeforeEach
	public void initMocks()
	{
		openMocks(this);
	}

	@Test
	public void testMockExists()
	{
		assertNotNull(job);
	}
}

I've attached a Maven project which demonstrates this flaw. Downgrading the Mockito version to 5.1.1 fixes the issue.

mockito-5.2.0-bug.zip

@TimvdLippe
Copy link
Contributor

Probably a regression introduced in #2923. @jfrantzius can you please take a look?

@jfrantzius
Copy link
Contributor

Looking at it...

@jfrantzius
Copy link
Contributor

jfrantzius commented Apr 1, 2023

That's an interesting somehow recursive construct that I'm surprised the compiler accepts, but then there surely shouldn't be a ClassCastException anyway ...

public class ConcreteJob extends AbstractJob<ConcreteJob>

(a class that uses itself as a type parameter for its superclass? 🤯 )

jfrantzius pushed a commit to jfrantzius/mockito that referenced this issue Apr 2, 2023
jfrantzius pushed a commit to jfrantzius/mockito that referenced this issue Apr 3, 2023
@jfrantzius
Copy link
Contributor

@TimvdLippe WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants