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

Investigate. Spring unit tests can't support autowired collection #2412

Closed
tepa46 opened this issue Jul 13, 2023 · 2 comments
Closed

Investigate. Spring unit tests can't support autowired collection #2412

tepa46 opened this issue Jul 13, 2023 · 2 comments
Assignees
Labels
comp-spring Issue is related to Spring projects support comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug Issue is a bug

Comments

@tepa46
Copy link
Collaborator

tepa46 commented Jul 13, 2023

To Reproduce

Create a project from the following classes:

  1. CollectionConfig
@Configuration
public class CollectionConfig {
    @Bean
    public List<String> nameList() {
        return Arrays.asList("John", "Adam", "Harry");
    }
}
  1. CollectionsService
@Component
public class CollectionsService {

    @Autowired
    private List<String> nameList;

    public int listSize(){
        return nameList.size();
    }
}

Try to generate tests for listSize method.

Expected behavior

nameList is Autowired collection. We expect to see this in the test class:

@Mock
    private List<String> nameList;

Actual behavior

@Test
    public void testListSize_ListSize() throws Exception {
        CollectionsBean collectionsBean = ((CollectionsBean) createInstance("com.rest.order.collection.CollectionsBean"));
        ArrayList arrayList = new ArrayList();
        arrayList.add(null);
        arrayList.add(null);
        arrayList.add(null);
        setField(collectionsBean, "com.rest.order.collection.CollectionsBean", "nameList", arrayList);

        int actual = collectionsBean.listSize();

        assertEquals(3, actual);
    }

Now we don't have Autowired nameList collection in the test class.

Additional context

The problem is in the following code that does not know that we need to mock current collection

val dependentMockModels =
            thisInstancesDependentModels
                .filterTo(mutableSetOf()) { cgModel ->
                    cgModel.model.isMockModel() && cgModel !in thisInstanceModels
                }
@tepa46 tepa46 added the ctg-bug Issue is a bug label Jul 13, 2023
@EgorkaKulikov EgorkaKulikov self-assigned this Jul 13, 2023
@EgorkaKulikov EgorkaKulikov added comp-spring Issue is related to Spring projects support comp-symbolic-engine Issue is related to the symbolic execution engine labels Jul 13, 2023
@EgorkaKulikov EgorkaKulikov added this to the Spring Phase 4 milestone Jul 13, 2023
@alisevych
Copy link
Member

alisevych commented Jul 17, 2023

At first:

  • investigate how to mock Collection of elements when it is autowired
    How to extend Mock model in Symbolic engine with Spring-specific cases.
    isMock field in UtModel

@alisevych alisevych changed the title Spring unit tests can't support autowired collection Investigate. Spring unit tests can't support autowired collection Jul 17, 2023
@EgorkaKulikov
Copy link
Collaborator

The following is required #2436

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-spring Issue is related to Spring projects support comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug Issue is a bug
Projects
Archived in project
Development

No branches or pull requests

3 participants