Skip to content

Wrong mocking for fields in Concrete Execution #2105

Closed
@Damtev

Description

@Damtev

Description

The Concrete Executor works incorrectly with mocked fields in <init> of classes different from the CUT. For example, consider the following code:

class ClassWithRandomField {
    public Random random = new Random();

    public int nextInt() {
        return random.nextInt();
    }
}

public class ClassUsingClassWithRandomField {
    public int useClassWithRandomField() {
        ClassWithRandomField classWithRandomField = new ClassWithRandomField();

        return classWithRandomField.nextInt();
    }
}

When generating tests for the ClassUsingClassWithRandomField#useClassWithRandomField method (with no mocking strategy but with java.util.Random included to mockAlways classes), the symbolic engine mocks constructors of the java.util.Random class and its method nextInt. So, the value returned from the MUT should equal the value returned by the mocked method nextInt. But in the concrete execution the random field becomes not mocked, which leads to using the real nextInt method and a diversion with the symbolic engine, and, as a result, an incorrect test.

To Reproduce

  1. Run generating tests action for the method useClassWithRandomField with no mocks strategy but with java.util.Random included to mockAlways classes
  2. Run a generated test.

Expected behavior

Generated test always passes.

Actual behavior

Almost always (sometimes the real nextInt value may return the same value as what is supposed to be mocked) generated test fails.

Environment

No extra environment.

Additional context

No additional context.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions