Skip to content

Use deep equality for mock results #1885

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 1 commit into from
Mar 3, 2023
Merged

Conversation

Damtev
Copy link
Member

@Damtev Damtev commented Mar 2, 2023

Description

If a returned value in the method under test is mocked, we cannot use assertEquals because the equals method for mocks works differently. So, we need to use deep equality in such cases.

Fixes #1877.

How to test

Automated tests

Common pipeline.

Manual tests

The same as in the corresponding issue.

Generated test:

///region SYMBOLIC EXECUTION: SUCCESSFUL EXECUTIONS for method provideObject()

/**
 * @utbot.classUnderTest {@link ProviderImpl}
 * @utbot.methodUnderTest {@link ProviderImpl#provideObject()}
 * @utbot.returnsFrom {@code return new ExampleClass(0);}
 */
@Test
@DisplayName("provideObject: -> return new ExampleClass(0)")
public void testProvideObject_Return() {
    org.mockito.MockedConstruction mockedConstruction = null;
    try {
        java.util.concurrent.atomic.AtomicInteger mockClassCounter = new java.util.concurrent.atomic.AtomicInteger();
        mockedConstruction = mockConstruction(ExampleClass.class, (ExampleClass ExampleClassMock, org.mockito.MockedConstruction.Context context) -> {
            switch (mockClassCounter.get()) {
                case 0:
                    break;
            }
            mockClassCounter.getAndIncrement();
        });
        ProviderImpl providerImpl = new ProviderImpl();

        ExampleClass actual = providerImpl.provideObject();

        ExampleClass expectedMock = mock(ExampleClass.class);
        expectedMock.field = 0;
        int expectedMockField = expectedMock.field;
        int actualField = actual.field;
        assertEquals(expectedMockField, actualField);

    } finally {
        mockedConstruction.close();
    }
}
///endregion

As can be seen, deep equality is used here.

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.

@Damtev Damtev added comp-codegen Issue is related to code generator ctg-bug-fix PR is fixing a bug labels Mar 2, 2023
@Damtev Damtev requested a review from EgorkaKulikov March 2, 2023 15:26
@Damtev Damtev merged commit 8fb6764 into main Mar 3, 2023
@Damtev Damtev deleted the damtev/use_deep_equals_for_mocks branch March 3, 2023 08:53
@alisevych alisevych modified the milestone: 2023.03 Release Mar 3, 2023
@alisevych alisevych added this to the 2023.03 Release milestone Mar 21, 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 ctg-bug-fix PR is fixing a bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Use deepEquals to assert Objects if one (or both) of them is a mock
3 participants