UnitTestBot / UTBotJava

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

Avoid spying collection-like types with @Spy if it may mislead @InjectMocks behaviour #2642

Closed tepa46 closed 1 year ago

tepa46 commented 1 year ago

Description

If we have models of different types implementing Collection or Map, we should not construct fields of these models with @Spy annotation but just use setField for it. Otherwise it will cause problems with @InjectMocks in Mockito because injection by type is used and in generated tests both List<A> and List<B> are represented as List listSpy.

One of the possible scenarios to reproduce the bug we fix here is to generate tests for Owner class in spring-petclinic project with 100% fuzzing. Before this fix, several different spies were created but injected not as we expect after that.

Self-check list