Skip to content

Incorrect mock for a method returning List of entities #2107

@alisevych

Description

@alisevych

Description

Incorrect mock is generated for the method of a Repository that returns List
PetType is the entity.

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 PetTypeFormatter$parse
  4. Check compilation errors in the generated test class

Expected behavior

Some valid List of PetType should be instantiated and used in the mock.
Some valid String should be passed into the method call.

Actual behavior

There is arrayList word returned from mock. It is not defined anywhere in the test. It is used in several tests.
There is string word as the input parameter of the method parse call. It is not defined anywhere in the test either.

Visual proofs (screenshots, logs, images)

image

The following test is generated:

	///region Test suites for executable org.springframework.samples.petclinic.owner.PetTypeFormatter.parse

	///region SYMBOLIC EXECUTION: SUCCESSFUL EXECUTIONS for method parse(java.lang.String, java.util.Locale)

	/**
	 * @utbot.classUnderTest {@link PetTypeFormatter}
	 * @utbot.methodUnderTest {@link PetTypeFormatter#parse(String, Locale)}
	 * @utbot.invokes {@link OwnerRepository#findPetTypes()}
	 * @utbot.invokes {@link java.util.Collection#iterator()}
	 * @utbot.iterates iterate the loop {@code for(PetType type: findPetTypes)} once
	 */
	@Test
	@DisplayName("parse: OwnerRepositoryFindPetTypes -> return type")
	public void testParse_TypeGetNameEquals() throws ParseException, ClassNotFoundException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
		(when(ownerRepositoryMock.findPetTypes())).thenReturn(arrayList);

		PetType actual = petTypeFormatter.parse(string, ((Locale) null));

		String petTypeFormatterName = ((String) getFieldValue(petTypeFormatter, "org.springframework.samples.petclinic.model.NamedEntity", "name"));
		String actualName = ((String) getFieldValue(actual, "org.springframework.samples.petclinic.model.NamedEntity", "name"));
		assertEquals(petTypeFormatterName, actualName);

		Integer actualId = ((Integer) getFieldValue(actual, "org.springframework.samples.petclinic.model.BaseEntity", "id"));
		assertNull(actualId);

	}
	///endregion

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 bugpriority-blockerBug blocking some of the main featuresstatus-verifiedBug fix is verified

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions