Skip to content

Unexpected @Mock for Collection in Spring Controller tests #2170

@alisevych

Description

@alisevych

Description

Unexpected @mock for Collection and Page in Spring Controller tests.

collectionMock is never used

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA
  2. Open spring-petclinic project
  3. Generate tests for VetController

Expected behavior

There should be fields annotated with @Mock for the used beans only.

Actual behavior

There are additional fields annotated with @Mock for java.util.Collection and org.springframework.data.domain.Page
collectionMock is never used.
pageMock is used in 1 error-prone test.

Screenshots, logs

import org.mockito.Mock;
import java.util.Collection;
import org.springframework.data.domain.Page;
import java.util.List;

public final class VetControllerTest {
	@InjectMocks
	public VetController vetController;

	@Mock
	public VetRepository vetRepositoryMock;

	@Mock
	public Collection collectionMock;

	@Mock
	public Page pageMock;

	public AutoCloseable mockitoCloseable;
...
}

Mocks of Page is used in the following test:

	///region OTHER: ERROR SUITE for method showVetList(int, org.springframework.ui.Model)

	@Test
	public void testShowVetList1() {
		org.mockito.MockedConstruction mockedConstruction = null;
		org.mockito.MockedStatic mockedStatic = null;
		try {
			ArrayList arrayList = new ArrayList();
			arrayList.add(null);
			arrayList.add(null);
			arrayList.add(null);
			arrayList.add(null);
			arrayList.add(null);
			arrayList.add(null);
			mockedConstruction = mockConstruction(Vets.class, (Vets vetsMock, org.mockito.MockedConstruction.Context context) -> (when(vetsMock.getVetList())).thenReturn(arrayList));
			mockedStatic = mockStatic(PageRequest.class);
			(mockedStatic.when(() -> PageRequest.of(anyInt(), anyInt()))).thenReturn(((PageRequest) null));
			ArrayList arrayList1 = new ArrayList();
			arrayList1.add(null);
			arrayList1.add(null);
			arrayList1.add(null);
			(when(pageMock.toList())).thenReturn(arrayList1);
			(when(vetRepositoryMock.findAll(any()))).thenReturn(pageMock);
			Model modelMock = mock(Model.class);

            /* This test fails because method [org.springframework.samples.petclinic.vet.VetController.showVetList] produces [org.mockito.exceptions.base.MockitoException: \nCannot call abstract real method on java object!\nCalling real methods is only possible when mocking non abstract method.\n  //correct example:\n  when(mockOfConcreteClass.nonAbstractMethod()).thenCallRealMethod();]
                org.utbot.instrumentation.instrumentation.execution.constructors.MockValueConstructor$generateMockitoAnswer$1.answer(MockValueConstructor.kt:178)
                org.springframework.samples.petclinic.vet.VetController.addPaginationModel(VetController.java:56)
                org.springframework.samples.petclinic.vet.VetController.showVetList(VetController.java:51) */
			vetController.showVetList(0, modelMock);
		} finally {
			mockedConstruction.close();
			mockedStatic.close();
		}
	}
	///endregion

Environment

IntelliJ IDEA version - 2023.1 Ultimate
Project - opened as Maven
JDK - 17

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorcomp-springIssue is related to Spring projects supportctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions