-
Notifications
You must be signed in to change notification settings - Fork 45
Closed
Labels
comp-codegenIssue is related to code generatorIssue is related to code generatorcomp-springIssue is related to Spring projects supportIssue is related to Spring projects supportctg-bugIssue is a bugIssue is a bug
Milestone
Description
Description
Unexpected @mock for Collection and Page in Spring Controller tests.
collectionMock is never used
To Reproduce
- Install UnitTestBot plugin built from main in IntelliJ IDEA
- Open
spring-petclinic
project - 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 generatorIssue is related to code generatorcomp-springIssue is related to Spring projects supportIssue is related to Spring projects supportctg-bugIssue is a bugIssue is a bug
Type
Projects
Status
Done