Skip to content

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

Open

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.

Activity

  1. added
    ctg-bugIssue is a bug
    comp-codegenIssue is related to code generator
    priority-blockerBug blocking some of the main features
    comp-springIssue is related to Spring projects support
    on Oct 3, 2023
  2. added this to the October Release milestone on Oct 3, 2023
  3. EgorkaKulikov commented on Oct 3, 2023

    @EgorkaKulikov
    Collaborator

    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.

  4. EgorkaKulikov commented on Oct 4, 2023

    @EgorkaKulikov
    Collaborator

    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 mockito/mockito#1066.

  5. alisevych commented on Oct 4, 2023

    @alisevych
    MemberAuthor

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

  6. EgorkaKulikov commented on Oct 4, 2023

    @EgorkaKulikov
    Collaborator

    @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.

  7. removed this from the October Release milestone on Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions