UTBotJava icon indicating copy to clipboard operation
UTBotJava copied to clipboard

UtUtils methods using setAccessible doesn't work in Java 17

Open alisevych opened this issue 3 years ago • 2 comments

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 avatar Mar 23 '23 14:03 alisevych

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

alisevych avatar Mar 23 '23 15:03 alisevych

"-ea" looks irrelevant, it just enables assertions

Vassiliy-Kudryashov avatar Sep 20 '23 14:09 Vassiliy-Kudryashov