Skip to content

ClassCastException when combining InjectMocks with generified Mock field #2958

@jsteenbeeke

Description

@jsteenbeeke

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions