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

HashCode of a mocked object is used in path constraints #286

Open
alisevych opened this issue Jun 23, 2022 · 1 comment
Open

HashCode of a mocked object is used in path constraints #286

alisevych opened this issue Jun 23, 2022 · 1 comment
Assignees
Labels
comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug Issue is a bug

Comments

@alisevych
Copy link
Member

alisevych commented Jun 23, 2022

Description

DoNotMockHashCode is failing on ExampleClass. hashCode of the mocked object is returning.

To Reproduce

Steps to reproduce the behavior:

  1. Run IntelliJ IDEA
  2. Open UTBotJava project
  3. Open the utbot-sample/src/main/java/org/utbot/examples/mock/CommonMocksExample.java file
  4. Generate tests for doNotMockHashCode with "Other packages: Mockito" and "No static mocking"
  5. Run the generated test

Expected behavior

testDoNotMockHashCode_ExampleClassHashCode is passed

Actual behavior

testDoNotMockHashCode_ExampleClassHashCode fails on Assertion
hashcode of the mocked object is returned

Visual proofs (screenshots, logs, images)

hashCode is different in every run. Error message in log is like that:
expected: <1119242936> but was: <227290491>
Expected :1119242936
Actual :227290491

Environment

Java 8

Additional context

Even if we make ExampleClass::hashCode return a constant - there will be a random number in the test:

 @Override
    public int hashCode() {
        return 100 * 31;
    }

@korifey korifey moved this to Todo in UTBot Java Jun 23, 2022
@alisevych alisevych added the ctg-bug Issue is a bug label Jun 23, 2022
@alisevych alisevych added this to the Release preparation milestone Sep 7, 2022
@CaelmBleidd CaelmBleidd changed the title hashCode is being mocked HashCode of a mocked object is used in path constraints Sep 14, 2022
@CaelmBleidd
Copy link
Member

The problem occurs when we have a method that returns a hashcode of some mocked value or uses it in path conditions. In such cases, we will have path divergence since the engine takes information from the real methods of the value (we cannot mock hashcode and equals due to Mockito limitations), but in the tests, we will use the ones from the Mocked value => have failing tests.

I think the best way is to detect such situations inside the engine and generate resulting tests without assertion for expected values (with a remark about the reason).

But seems like it is something that is not easy to do, so I suggest removing this bug from the release milestone if we haven't seen such situations in real (users') code and fixing it later. What do you think, @alisevych?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug Issue is a bug
Projects
Status: Todo
Development

No branches or pull requests

2 participants