Skip to content

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

Closed
@tyuldashev

Description

@tyuldashev

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.

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions