Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No validation of a Collection changing passed as input parameter #2372

Open
alisevych opened this issue Jul 6, 2023 · 2 comments
Open

No validation of a Collection changing passed as input parameter #2372

alisevych opened this issue Jul 6, 2023 · 2 comments
Labels
comp-codegen Issue is related to code generator comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug Issue is a bug

Comments

@alisevych
Copy link
Member

Description

No validation of a Collection of elements in Spring No configuration test

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA 2023.1.3
  2. Open spring-petclinic project
  3. Generate tests for Owner class
  4. Check successful test on (pet.isNew()): True branch

Expected behavior

Assert added Pet exists in owner.pets field

Actual behavior

List of null values is created. No checks for the List are made.

Screenshots, logs

/**
	 * @utbot.classUnderTest {@link Owner}
	 * @utbot.methodUnderTest {@link Owner#addPet(Pet)}
	 * @utbot.executesCondition {@code (pet.isNew()): True}
	 * @utbot.invokes {@link Owner#getPets()}
	 * @utbot.invokes {@link java.util.List#add(Object)}
	 */
	@Test
	@DisplayName("addPet: pet.isNew() : True -> ListAdd")
	public void testAddPet_PetIsNew() {
		ArrayList arrayList = new ArrayList();
		arrayList.add(null);
		arrayList.add(null);
		arrayList.add(null);
		Pet petMock1 = mock(Pet.class);
		(when(petMock1.isNew())).thenReturn(true);

		owner.addPet(petMock1);
	}

Environment

IntelliJ IDEA version - Ultimate 2023.1.3
Project - Gradle
JDK - 17

@alisevych alisevych added ctg-bug Issue is a bug comp-symbolic-engine Issue is related to the symbolic execution engine comp-spring Issue is related to Spring projects support labels Jul 6, 2023
@alisevych alisevych added the comp-codegen Issue is related to code generator label Jul 6, 2023
@EgorkaKulikov
Copy link
Collaborator

I created the non-Spring project containing two classes:

  1. Pet
public class Pet {
    public String name;
}

2.Service

import java.util.ArrayList;

public class Service {
    public ArrayList<Pet> pets;

    public void addPet(Pet pet) {
        pets.add(pet);
    }
}

and generated tests for addPet method.

The following tests were generated:

public final class ServiceTest {

    @Test
    @DisplayName("addPet: -> ArrayListAdd")
    public void testAddPet_ArrayListAdd() {
        Service service = new Service();
        ArrayList arrayList = new ArrayList();
        arrayList.add(null);
        arrayList.add(null);
        arrayList.add(null);
        service.pets = arrayList;

        service.addPet(null);
    }
    
    @Test
    @DisplayName("addPet: pets.add(pet) : True -> ThrowNullPointerException")
    public void testAddPet_ArrayListAdd_1() {
        Service service = new Service();
        
        /* This test fails because method [pack.Service.addPet] produces [java.lang.NullPointerException]
            pack.Service.addPet(Service.java:10) */
        service.addPet(null);
    }
}

There are no asserts on pets field state...

@alisevych
Copy link
Member Author

It would be great to check if UtExecution contains the information on changing field with Collection.
It can be accessed by method Owner#getPets() call to validate the changes from the test.

@alisevych alisevych removed the comp-spring Issue is related to Spring projects support label Jul 11, 2023
@alisevych alisevych self-assigned this Jul 17, 2023
@alisevych alisevych changed the title No validation of a Collection of elements in Spring No configuration test No validation of a Collection changing passed as input parameter Sep 20, 2023
@alisevych alisevych removed their assignment Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug Issue is a bug
Projects
Status: Todo
Development

No branches or pull requests

2 participants