Skip to content

Mock for dependent list is created but not injected into entity #2637

Open
@alisevych

Description

@alisevych

Description

linkedListSpy is created and configured, but not injected into Entity under test

To Reproduce

  1. Install -IU- UnitTestBot plugin in IntelliJ IDEA
  2. Open spring-petclinic
  3. Generate Unit tests for Owner with PetClinicApplication configuration
  4. Run the tests

Expected behavior

No NPE should be thrown from get-methods of the entity before assertions.

Actual behavior

There are several tests throwing NPE when getting fields of actual Pet.

Screenshots, logs

	@Test
	@DisplayName("getPet: id = zero (mutated from min)")
	public void testGetPetWithCornerCase5() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
		owner.setLastName("-3");
		Pet petMock = mock(Pet.class);
		(when(petMock.isNew())).thenReturn(true);
		linkedListSpy.add(petMock);
		Pet petMock1 = mock(Pet.class);
		(when(petMock1.isNew())).thenReturn(false);
		(when(petMock1.getId())).thenReturn(0);
		linkedListSpy.add(petMock1);
		Pet petMock2 = mock(Pet.class);
		linkedListSpy.add(petMock2);
		Pet petMock3 = mock(Pet.class);
		linkedListSpy.add(petMock3);
		Pet petMock4 = mock(Pet.class);
		linkedListSpy.add(petMock4);

		Pet actual = owner.getPet(0);

		LocalDate actualBirthDate = actual.getBirthDate();
		assertNull(actualBirthDate);

		PetType actualType = actual.getType();
		assertNull(actualType);

		Set actualVisits = ((Set) getFieldValue(actual, "org.springframework.samples.petclinic.owner.Pet", "visits"));
		assertNull(actualVisits);

		String actualName = actual.getName();
		assertNull(actualName);

		Integer actualId = actual.getId();
		assertNull(actualId);

	}

image

image

Environment

IntelliJ IDEA version - Community 2023.2
Project - Maven
JDK - 17

Additional context

When debugging, linkedListSpy is returning expected values.
There is pet returning isNew()=false and getId()=0.
But owner.getPets() returns empty list.

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

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions