Skip to content

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

Open
@alisevych

Description

@alisevych

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

Metadata

Metadata

Labels

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

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions