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

UtBot mocks interface even when Do not mock is selected #1560

Closed
tyuldashev opened this issue Dec 20, 2022 · 2 comments · Fixed by #1889
Closed

UtBot mocks interface even when Do not mock is selected #1560

tyuldashev opened this issue Dec 20, 2022 · 2 comments · Fixed by #1889
Assignees
Labels
comp-codegen Issue is related to code generator ctg-bug Issue is a bug

Comments

@tyuldashev
Copy link
Collaborator

Description
UtBot tries to mock parameter which type is interface without existing implementations. As Do not mock selected no Mockito dependencies are added and so generated code cannot be executed.

To Reproduce

Steps to reproduce the behavior:

  1. Open some test project which has NO Mockito dependencies in it
  2. Generate tests for code below, select Do not mock option
public class CommonMocksExample {
    public Object mockInterfaceWithoutImplementors(InterfaceWithoutImplementors value) {
        return value.visit(this);
    }

}

interface InterfaceWithoutImplementors {
    Object visit(Object value);
}

Expected behavior

Generated tests have no mocks and can be executed. Or no tests are generated.

Actual behavior

Generated tests shown below. It contains references to Mockito when and actual, but because there is no Mockito dependency in the project, such tests cannot be executed.

    @Test
    @DisplayName("mockInterfaceWithoutImplementors: InterfaceWithoutImplementorsVisit -> return value.visit(this)")
    public void testMockInterfaceWithoutImplementors_InterfaceWithoutImplementorsVisit() {
        CommonMocksExample commonMocksExample = new CommonMocksExample();
        InterfaceWithoutImplementors valueMock = mock(InterfaceWithoutImplementors.class);
        (when(valueMock.visit(any()))).thenReturn(null);

        Object actual = commonMocksExample.mockInterfaceWithoutImplementors(valueMock);

        assertNull(actual);
    }

Visual proofs (screenshots, logs, images)
image

Environment
IDEA 2022.2.3
UTBot 2022.12.637 from unit-test-bot/rc16122022 branch

Additional context

That seems quite old bug, as it's reproducible in 2022.10.580 version of UTBot too.

@tyuldashev tyuldashev added the ctg-bug Issue is a bug label Dec 20, 2022
@tyuldashev tyuldashev moved this to Todo in UTBot Java Dec 20, 2022
@tyuldashev tyuldashev added the comp-codegen Issue is related to code generator label Feb 28, 2023
@tyuldashev
Copy link
Collaborator Author

@CaelmBleidd Do you think some simple fix is possible here? Or the fix would definitely require significant amount of instigation/work?

Regarding expected result: in case we have to mock in this situation then we need to add dependency to avoid red code.

@CaelmBleidd
Copy link
Member

No, I believe, it requires either a little work or no work at all. We have a similar mechanism for so-called force mocking, implemented inside org.utbot.engine.util.mockListeners.MockListenerController and org.utbot.engine.util.mockListeners.MockListener with its inheritors. AFAIR, it removes executions from the final set if they touched something that requires Mockito during the analysis.

Probably, it'd be enough to just add in the org.utbot.engine.Mocker#forceMock function a similar code from the org.utbot.engine.Mocker#shouldMock.

@EgorkaKulikov please, take a look at it. Am I right that it is enough to solve the problem?

@alisevych alisevych added this to the 2023.03 Release milestone Mar 3, 2023
@EgorkaKulikov EgorkaKulikov linked a pull request Mar 13, 2023 that will close this issue
6 tasks
@github-project-automation github-project-automation bot moved this from Todo to Done in UTBot Java Mar 13, 2023
denis-fokin pushed a commit that referenced this issue Mar 13, 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 Issue is a bug
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants