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

UtUtils methods using setAccessible doesn't work in Java 17 #2026

Open
alisevych opened this issue Mar 23, 2023 · 2 comments
Open

UtUtils methods using setAccessible doesn't work in Java 17 #2026

alisevych opened this issue Mar 23, 2023 · 2 comments
Assignees
Labels
comp-codegen Issue is related to code generator ctg-bug Issue is a bug

Comments

@alisevych
Copy link
Member

Description

UtUtils methods using setAccessible doesn't work in Java 17
That results in failing successful executions.

To Reproduce

  1. Run the 'utbot' project in IntelliJ Idea 2022.3 Ultimate
  2. Install plugin built from unit-test-bot/rc2023.3 branch
  3. Create a new project with JDK 17 (f.i. Maven)
  4. Add the following dependency to pom.xml:
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
  1. Copy-paste mixed\LoggerExample.java into it
  2. Generate and Run tests with mocks + static mocks (default settings)

Expected behavior

Tests classified as Successful should pass.

Actual behavior

3 successful executions generated by Symbolic engine - fail.
The following error is produced by UtUtils.getStaticFieldValue() :

java.lang.reflect.InaccessibleObjectException: Unable to make private native java.lang.reflect.Field[] java.lang.Class.getDeclaredFields0(boolean) accessible: module java.base does not "opens java.lang" to unnamed module @5afa04c

Visual proofs (screenshots, logs, images)

    @Test
    @DisplayName("loggerUsage: -> return 2")
    public void testLoggerUsage_Return2() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
        org.mockito.MockedStatic mockedStatic = null;
        try {
            Class loggerExampleClazz = Class.forName("org.utbot.examples.mixed.LoggerExample");
            Logger prevLogger = ((Logger) getStaticFieldValue(loggerExampleClazz, "logger"));
            try {
                Logger loggerMock = mock(Logger.class);
                (when(loggerMock.isDebugEnabled())).thenReturn(false);
                setStaticField(loggerExampleClazz, "logger", loggerMock);
                mockedStatic = mockStatic(org.slf4j.LoggerFactory.class);
                (mockedStatic.when(() -> org.slf4j.LoggerFactory.getLogger(any(Class.class)))).thenReturn(loggerMock);
                LoggerExample loggerExample = new LoggerExample();

                int actual = loggerExample.loggerUsage();

                assertEquals(2, actual);
            } finally {
                setStaticField(LoggerExample.class, "logger", prevLogger);
            }
        } finally {
            mockedStatic.close();
        }
    }

Environment

IntelliJ IDEA 2022.3.3 Ultimate
JDK 17

Additional context

The same tests generated in UTBotJava project pass.
That happens because of specially added list of "--add-opens" keys to jvmArgs.

When the following keys are added to the test run configuration in the new project - the tests pass.
-ea --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED

@alisevych alisevych added the ctg-bug Issue is a bug label Mar 23, 2023
@alisevych alisevych added this to the 2023.03 Release milestone Mar 23, 2023
@alisevych
Copy link
Member Author

alisevych commented Mar 23, 2023

Suggested temporary solution:
add comments to the tests that use reflection - that run configuration should be corrected manually

@Vassiliy-Kudryashov
Copy link
Member

"-ea" looks irrelevant, it just enables assertions

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