Skip to content

Incorrect cast to String in mock definition for a Controller #2104

@alisevych

Description

@alisevych

Description

Controller is cast to String in the mock definition for spring-petclinic project.

To Reproduce

  1. Open as Gradle the spring-petclinic project in IntelliJ Idea 2022.3.3
  2. Install the latest plugin built from main
  3. Generate tests for OwnerController
  4. Check compilation errors in the generated test class

Expected behavior

There should not be casts of Controller classes to String.
Also OwnerController is added as a field with @Injectmocks annotation.

Actual behavior

There are two tests in ERROR SUITE where Controller is cast to String in the mock definition.
1 - in SYMBOLIC EXECUTION: ERROR SUITE for method processFindForm
2 - in OTHER: ERROR SUITE for method processFindForm

Visual proofs (screenshots, logs, images)

public final class OwnerControllerTest {

	@InjectMocks
	public OwnerController ownerController;

	/**
	 * @utbot.classUnderTest {@link OwnerController}
	 * @utbot.methodUnderTest {@link OwnerController#processFindForm(int, Owner, BindingResult, Model)}
	 * @utbot.executesCondition {@code (owner.getLastName() == null): False}
	 * @utbot.throwsException {@link NullPointerException} in: ownersResults.isEmpty()
	 */
	@Test
	@DisplayName("processFindForm: ownersResults.isEmpty() -> ThrowNullPointerException")
	public void testProcessFindForm_OwnerGetLastNameNotEqualsNull() {
		org.mockito.MockedStatic mockedStatic = null;
		try {
			mockedStatic = mockStatic(PageRequest.class);
			(mockedStatic.when(() -> PageRequest.of(anyInt(), anyInt()))).thenReturn(((PageRequest) null));
            /* WARNING!!! Automatically used "Mockito static mocking" framework for mocking statics
            because execution encountered flaky methods
            To change this behaviour edit [Settings -> UtBot -> Force static mocking] */
			(when(ownerRepositoryMock.findByLastName(any(), any()))).thenReturn(((Page) null));
			Owner ownerMock = mock(Owner.class);
			(when(ownerMock.getLastName())).thenReturn(((String) ownerController), ((String) null));

			/* This test fails because method [org.springframework.samples.petclinic.owner.OwnerController.processFindForm] produces [java.lang.NullPointerException] */
			ownerController.processFindForm(-255, ownerMock, null, null);
		} finally {
			mockedStatic.close();
		}
	}

	@Test
	public void testProcessFindForm2() {
		org.mockito.MockedStatic mockedStatic = null;
		try {
			mockedStatic = mockStatic(PageRequest.class);
			(mockedStatic.when(() -> PageRequest.of(anyInt(), anyInt()))).thenReturn(((PageRequest) null));
            /* WARNING!!! Automatically used "Mockito static mocking" framework for mocking statics
            because execution encountered flaky methods
            To change this behaviour edit [Settings -> UtBot -> Force static mocking] */
			(when(pageMock.isEmpty())).thenReturn(false);
			(when(pageMock.getTotalElements())).thenReturn(-9223372036854775806L);
			(when(ownerRepositoryMock.findByLastName(any(), any()))).thenReturn(pageMock);
			Owner ownerMock = mock(Owner.class);
			(when(ownerMock.getLastName())).thenReturn(((String) ownerController), ((String) null));

            /* This test fails because method [org.springframework.samples.petclinic.owner.OwnerController.processFindForm] produces [java.lang.NullPointerException]
                org.springframework.samples.petclinic.owner.OwnerController.addPaginationModel(OwnerController.java:114)
                org.springframework.samples.petclinic.owner.OwnerController.processFindForm(OwnerController.java:110) */
			ownerController.processFindForm(0, ownerMock, null, null);
		} finally {
			mockedStatic.close();
		}
	}

Environment

Windows 10 Pro
IntelliJ IDEA 2022.3.3 Ultimate
Project is opened as Gradle

Metadata

Metadata

Labels

comp-codegenIssue is related to code generatorcomp-springIssue is related to Spring projects supportctg-bugIssue is a bugstatus-verifiedBug fix is verified

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions