Skip to content

Call doNothing from mockito in tests for method calls returning void #2016

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
Mar 27, 2023

Conversation

EgorkaKulikov
Copy link
Collaborator

Description

Consider the following code

private EntityManager em;

public void add(User user) {
        em.persist(user);
    }

Test code looks as follows:

@InjectMocks
  public UserDaoImp userDaoImp;

  @Mock
  public EntityManager entityManagerMock;

public void testAdd_EntityManagerPersist() {
      userDaoImp.add(null);
  }

It is better to see

public void testAdd_EntityManagerPersist() {
        doNothing().when(entityManagerMock).persist(any());
        userDaoImp.add(null);
    }

as entityManagerMock variable is considered to be unused by Idea that suggests to remove it. However, variable is useful, test will fail without it.

How to test

Should not be tested as a standalone functionality, just as a part of Spring 1st phase full testing

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.

@EgorkaKulikov EgorkaKulikov added ctg-enhancement New feature, improvement or change request comp-codegen Issue is related to code generator labels Mar 22, 2023
@EgorkaKulikov EgorkaKulikov force-pushed the egor/mockito_donothing branch from 918e80b to f3e530e Compare March 22, 2023 14:09
@sofurihafe sofurihafe force-pushed the egor/mockito_donothing branch from 350bcea to e5a874f Compare March 26, 2023 01:03
@EgorkaKulikov EgorkaKulikov requested a review from Damtev March 27, 2023 07:01
@sofurihafe sofurihafe force-pushed the egor/mockito_donothing branch from 4c38b02 to ef45448 Compare March 27, 2023 10:50
@sofurihafe sofurihafe requested a review from Damtev March 27, 2023 10:50
@sofurihafe sofurihafe enabled auto-merge (squash) March 27, 2023 10:55
@sofurihafe sofurihafe merged commit 71a71e1 into main Mar 27, 2023
@sofurihafe sofurihafe deleted the egor/mockito_donothing branch March 27, 2023 16:25
@alisevych alisevych added the comp-spring Issue is related to Spring projects support label Mar 30, 2023
@alisevych alisevych added this to the Spring Phase 1 milestone Apr 12, 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
None yet
Development

Successfully merging this pull request may close these issues.

4 participants