UnitTestBot / UTBotJava

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

Integration tests on OwnerController check entities that do not persist #2623

Closed alisevych closed 1 year ago

alisevych commented 1 year ago

Description

OwnerController Integration tests fail on assertions. findOwner tests get Owner by id, but it was not saved in the test method before.

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA
  2. Open spring-petclinic project
  3. Generate Integration tests for OwnerController
  4. Run the tests

Expected behavior

Tests should fail only on expected exceptions. Entity should be saved to database before calling methods that check it.

Actual behavior

There are 26 tests failing unexpectedly. There are tests that call ownerController.findOwner(<int>), but do not call entityManager.persist(...) before.

Screenshots, logs

This is one of successful tests on findOwner:

    /**
     * @utbot.classUnderTest {@link OwnerController}
     * @utbot.methodUnderTest {@link OwnerController#findOwner(Integer)}
     */
    @Test
    @DisplayName("findOwner: ownerId = 3 (mutated from positive)")
    public void testFindOwner1() throws Exception {
        Owner actual = ownerController.findOwner(3);

        Owner expected = ((Owner) createInstance("org.springframework.samples.petclinic.owner.Owner"));
        String address = "2693 Commerce St.";
        expected.setAddress(address);
        String city = "McFarland";
        expected.setCity(city);
        String telephone = "6085558763";
        expected.setTelephone(telephone);
        List pets = new ArrayList();
        Pet pet = ((Pet) createInstance("org.springframework.samples.petclinic.owner.Pet"));
        LocalDate birthDate = ((LocalDate) createInstance("java.time.LocalDate"));
        pet.setBirthDate(birthDate);
        PetType type = ((PetType) createInstance("org.springframework.samples.petclinic.owner.PetType"));
        String name = "dog";
        type.setName(name);
        Integer id = 2;
        type.setId(id);
        pet.setType(type);
        Set visits = new LinkedHashSet();
        setField(pet, "org.springframework.samples.petclinic.owner.Pet", "visits", visits);
        String name1 = "Jewel";
        pet.setName(name1);
        Integer id1 = 4;
        pet.setId(id1);
        pets.add(pet);
        Pet pet1 = ((Pet) createInstance("org.springframework.samples.petclinic.owner.Pet"));
        LocalDate birthDate1 = ((LocalDate) createInstance("java.time.LocalDate"));
        pet1.setBirthDate(birthDate1);
        pet1.setType(type);
        Set visits1 = new LinkedHashSet();
        setField(pet1, "org.springframework.samples.petclinic.owner.Pet", "visits", visits1);
        String name2 = "Rosy";
        pet1.setName(name2);
        Integer id2 = 3;
        pet1.setId(id2);
        pets.add(pet1);
        setField(expected, "org.springframework.samples.petclinic.owner.Owner", "pets", pets);
        String firstName = "Eduardo";
        expected.setFirstName(firstName);
        String lastName = "Rodriquez";
        expected.setLastName(lastName);
        expected.setId(id2);

        String expectedAddress = expected.getAddress();
        String actualAddress = actual.getAddress();
        assertEquals(expectedAddress, actualAddress);

        String expectedCity = expected.getCity();
        String actualCity = actual.getCity();
        assertEquals(expectedCity, actualCity);

        String expectedTelephone = expected.getTelephone();
        String actualTelephone = actual.getTelephone();
        assertEquals(expectedTelephone, actualTelephone);

        List expectedPets = expected.getPets();
        List actualPets = actual.getPets();
        assertTrue(deepEquals(expectedPets, actualPets));

        String expectedFirstName = expected.getFirstName();
        String actualFirstName = actual.getFirstName();
        assertEquals(expectedFirstName, actualFirstName);

        String expectedLastName = expected.getLastName();
        String actualLastName = actual.getLastName();
        assertEquals(expectedLastName, actualLastName);

        Integer expectedId = expected.getId();
        Integer actualId = actual.getId();
        assertEquals(expectedId, actualId);

    }
    ///region FUZZER: CHECKED EXCEPTIONS for method showOwner(int)

    /**
     * @utbot.classUnderTest {@link OwnerController}
     * @utbot.methodUnderTest {@link OwnerController#showOwner(int)}
     */
    @Test
    @DisplayName("showOwner: ownerId = zero (mutated from min) -> throw ServletException")
    public void testShowOwnerThrowsSEWithCornerCase() throws Exception {
        UriComponentsBuilder uriComponentsBuilder = fromPath("/owners/{ownerId}");
        Map uriVariables = new HashMap();
        uriVariables.put("ownerId", 0);
        UriComponentsBuilder uriComponentsBuilder1 = uriComponentsBuilder.uriVariables(uriVariables);
        String urlTemplate = uriComponentsBuilder1.toUriString();
        Object[] uriVariables1 = {};
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = get(urlTemplate, uriVariables1);

        /* This test fails because method [org.springframework.test.web.servlet.MockMvc.perform] produces [jakarta.servlet.ServletException: Request processing failed: java.lang.IllegalArgumentException: Model object must not be null]
            org.springframework.util.Assert.notNull(Assert.java:204)
            org.springframework.ui.ModelMap.addAttribute(ModelMap.java:90)
            org.springframework.web.servlet.ModelAndView.addObject(ModelAndView.java:300)
            org.springframework.samples.petclinic.owner.OwnerController.showOwner(OwnerController.java:157) */
        mockMvc.perform(mockHttpServletRequestBuilder);
    }

Environment

IntelliJ IDEA version - Ultimate 2023.2 Project - spring-petclinic (Gradle) JDK - 17

alisevych commented 1 year ago

As discussed with @IlyaMuravjov , entities are created in spring-petclinic database by default. So closing as not a bug