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

Unneeded static final field reassignment via Reflection #1774

Open
alisevych opened this issue Feb 7, 2023 · 0 comments
Open

Unneeded static final field reassignment via Reflection #1774

alisevych opened this issue Feb 7, 2023 · 0 comments
Assignees
Labels
comp-codegen Issue is related to code generator ctg-bug Issue is a bug

Comments

@alisevych
Copy link
Member

Description

There is a test generated for CommonMocksExample.clinitMockExample() which contains unneeded static final field reassignment with Reflection mechanism.

To Reproduce

  1. Run the 'utbot' project in IntelliJ Idea 2022.2.4
  2. Use plugin built from main to generate tests for CommonMocksExample.clinitMockExample()
  3. Open the test generated by Symbolic execution, successful

Expected behavior

Test just uses the default value of the field. And checks the behavior of the tested method clinitMockExample().

Actual behavior

The test reassigns the filed using reflection with the same default value.

Visual proofs (screenshots, logs, images)

    ///region Test suites for executable org.utbot.examples.mock.CommonMocksExample.clinitMockExample

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

    /**
     * @utbot.classUnderTest {@link CommonMocksExample}
     * @utbot.methodUnderTest {@link CommonMocksExample#clinitMockExample()}
     */
    @Test
    @DisplayName("clinitMockExample: ")
    public void testClinitMockExample() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
        Integer prevKeyValue = org.utbot.examples.objects.ObjectWithFinalStatic.keyValue;
        try {
            Integer keyValue = 420;
            Class objectWithFinalStaticClazz = Class.forName("org.utbot.examples.objects.ObjectWithFinalStatic");
            setStaticField(objectWithFinalStaticClazz, "keyValue", keyValue);
            CommonMocksExample commonMocksExample = new CommonMocksExample();

            int actual = commonMocksExample.clinitMockExample();

            assertEquals(-420, actual);
        } finally {
            setStaticField(org.utbot.examples.objects.ObjectWithFinalStatic.class, "keyValue", prevKeyValue);
        }
    }
    ///endregion

    ///endregion

Environment

Windows 10 Ppr
IntelliJ IDEA 2022.2.4
Gradle
JDK 11

@alisevych alisevych added ctg-bug Issue is a bug comp-codegen Issue is related to code generator labels Feb 7, 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
Status: Todo
Development

No branches or pull requests

2 participants