UTBotJava icon indicating copy to clipboard operation
UTBotJava copied to clipboard

Mock for dependent list is created but not injected into entity

Open alisevych opened this issue 2 years ago • 4 comments

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.

alisevych avatar Oct 03 '23 08:10 alisevych

Investigated with @tepa46 and @Vassiliy-Kudryashov - looks like a very strange case.

There is no obvious reasons why Spring cannot inject this Spy into the instance under test. It can be manually fixed if we replace ArrayList with ArrayList<Pet>, but we cannot generate such code due to generics support limitations in UnitTestBot.

Tomorrow I'm going to ask other collegues if they see the reasons of autowiring problems here. If we clarify it, it may be possible to use some fallback as we do when there are several lists of one type in project.

EgorkaKulikov avatar Oct 03 '23 15:10 EgorkaKulikov

It seems to be a bug in Mockito.

Consider the following test

        @Test
	@DisplayName("getPet: compId.equals(id) : True -> return pet")
	public void testGetPet_CompIdEquals() throws Exception {
		Pet petMock = mock(Pet.class);
		(when(petMock.isNew())).thenReturn(false);
		(when(petMock.getId())).thenReturn(-255);
		pets.add(petMock);
		pets.add(null);
		pets.add(null);
		Integer id = -255;

		Pet actual = owner.getPet(id);

		assertNull(actual.getBirthDate());
		assertNull(actual.getType());
		Set actualVisits = ((Set) getFieldValue(actual, "org.springframework.samples.petclinic.owner.Pet", "visits"));
		assertNull(actualVisits);
		assertNull(actual.getName());
		assertNull(actual.getId());
	}

This version does not work

        @InjectMocks
	private Owner owner;

	@Spy
	private ArrayList pets;

This version works

        @InjectMocks
	private Owner owner;

	private ArrayList pets = Mockito.spy(new ArrayList());

However, in accordance with @Spy documentation, it must be the same.

Seems to be a generics support problems in @InjectMocks. There are some other known issues about it, like https://github.com/mockito/mockito/issues/1066.

EgorkaKulikov avatar Oct 04 '23 08:10 EgorkaKulikov

@EgorkaKulikov Can we generate code like it is in your second example? With explicit spy creation?

alisevych avatar Oct 04 '23 09:10 alisevych

@Spy annotation was constructed as a smart syntax and guidelines recommend it. After that, here we know one seldom bug, I have no idea how many potential problems are hidden in it. It can be investigated, but it will take time.

EgorkaKulikov avatar Oct 04 '23 09:10 EgorkaKulikov