Skip to content

Support autowiring collections in Spring Unit tests #2473

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

Merged
merged 6 commits into from
Aug 3, 2023

Conversation

tepa46
Copy link
Collaborator

@tepa46 tepa46 commented Aug 2, 2023

Description

Fixes #2436

Add supporting autowiring collections by Spy annotation in Spring Unit tests.

The class under test:

@Service
public class OrderService {

    @Autowired
    private List<Person> lst;

    public Boolean checkLst() {
        return !lst.get(0).getName().equals("ABC");
    }

}

Generated test:

public final class OrderServiceTest {
    @InjectMocks
    private OrderService orderService;

    @Spy
    private ArrayList lstSpy;

    public void testCheckLst_NotLstGet0GetNameEquals_1() {
        Person personMock1 = mock(Person.class);
        String string1 = "ABC";
        (when(personMock1.getName())).thenReturn(string1);
        lstSpy.add(personMock1);

        Boolean actual = orderService.checkLst();

        Boolean expected = false;

        assertEquals(expected, actual);
    }
...
}

How to test

Manual tests

Manual tested cases:

  • List
  • Map
  • Set
  • Two and more collections of one type
  • Two and more collections of different type

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@tepa46 tepa46 added ctg-enhancement New feature, improvement or change request comp-codegen Issue is related to code generator comp-spring Issue is related to Spring projects support labels Aug 2, 2023
@tepa46 tepa46 requested a review from EgorkaKulikov August 2, 2023 15:54
.filterTo(mutableSetOf()) { cgModel ->
cgModel.model.canBeSpied() && cgModel !in thisInstanceModels
}

val autowiredFromContextModels =
stateBeforeDependentModels.filterTo(HashSet()) { it.model.isAutowiredFromContext() }

return SpringSpecificInformation(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be we should create a constructor of SpringSpecificInformation that groups by classId itself and here just write
SpringSpecificInformation(thisInstanceModels, dependentMockModels, dependentSpyModels, autowiredFromContextModels)

@EgorkaKulikov EgorkaKulikov merged commit 0e79564 into main Aug 3, 2023
@EgorkaKulikov EgorkaKulikov deleted the kirill/autowiring-collections-by-spy branch August 3, 2023 12:34
IlyaMuravjov added a commit that referenced this pull request Aug 4, 2023
EgorkaKulikov pushed a commit that referenced this pull request Aug 7, 2023
* Support creation and persistence of valid entities

* Make `GeneratedFieldValueProvider` be aware of entity class and not just field declaring class

* Introduce `PreservableFuzzedTypeProperty`

* Make so `persist()` and `detach()` modifications are always applied for MANAGED and DETACHED entities

* Actualize `HandlerClassesLoader.loadClass` javadoc

* Introduce value replacement for common collection types in `UtModelConstructor`

* Make minimization encourage use of non-hardcoded values

* Improve valid value providers

* Add `@WithMockUser` in concrete execution and code generation

* Fix compilation after rebase

* Fix inconsistent naming after rebase

* Fix inconsistent naming after one more rebase

* Fix `stateBeforeDependentModels` after #2473

* Make `getDependenciesForBean` actually use `analyzedBeanNames`

* Add `@WithMockUser` in code generation
@alisevych alisevych added this to the Spring Phase 4 milestone Aug 11, 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-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Support Spring unit tests generation with autowired collections
3 participants