UnitTestBot / UTBotJava

Automated unit test generation and precise code analysis for Java
Apache License 2.0
137 stars 43 forks source link

Incorrect assertNull is applied to the value returned from a mocked method #2639

Closed alisevych closed 1 year ago

alisevych commented 1 year ago

Description

assertNull is applied to the method returned value, while it's method is mocked. assertEquals to exact value is expected.

To Reproduce

  1. Install -IU- UnitTestBot plugin in IntelliJ IDEA
  2. Open spring-petclinic
  3. Set Fuzzing 100% mode
  4. Generate Unit tests for PetTypeFormatter with PetClinicApplication configuration
  5. Run the tests

Expected behavior

Method PetType$getName() is mocked with exact return value. assertEquals for the actualName is expected.

Actual behavior

There is failing assertNull for actualName in successful test.

Screenshots, logs

    @Test
    @DisplayName("parse: text = 'XZ', locale = Locale(String, String, String)")
    public void testParseWithNonEmptyString() throws ParseException {
        ArrayList arrayList = new ArrayList();
        PetType petTypeMock = mock(PetType.class);
        (when(petTypeMock.getName())).thenReturn("XZ");
        arrayList.add(petTypeMock);
        (when(ownerRepositoryMock.findPetTypes())).thenReturn(arrayList);
        Locale locale = new Locale("\n\t\r", "#$\\\"'", "#$\\\"'");

        PetType actual = petTypeFormatter.parse("XZ", locale);

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

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

image

Environment

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