GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — UtUtils methods using setAccessible doesn't work in Java 17 · Issue #2026 · UnitTestBot/UTBotJava

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

New issue
Copy link
New issue
Copy link
Open
Open
UtUtils methods using setAccessible doesn't work in Java 17#2026
Copy link
Assignees
Vassiliy-Kudryashov
Labels
comp-codegenIssue is related to code generatorIssue is related to code generatorctg-bugIssue is a bugIssue is a bug

Description

@alisevych
alisevych
opened on Mar 23, 2023
Issue body actions

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

Activity

  1. alisevych
    added
    ctg-bugIssue is a bugIssue is a bug
    on Mar 23, 2023
  2. alisevych
    added this to the 2023.03 Release milestone on Mar 23, 2023
  3. alisevych commented on Mar 23, 2023

    @alisevych
    alisevych
    on Mar 23, 2023
    MemberAuthor
    More actions

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

  4. alisevych
    assigned
    Vassiliy-Kudryashov
    on Mar 23, 2023
  5. korifey
    added this to UTBot Javaon Mar 23, 2023
  6. github-project-automation
    moved this to Todo in UTBot Javaon Mar 23, 2023
  7. alisevych
    added
    comp-codegenIssue is related to code generatorIssue is related to code generator
    on Mar 24, 2023
  8. alisevych
    removed this from the 2023.03 Release milestone on Mar 27, 2023
  9. Vassiliy-Kudryashov commented on Sep 20, 2023

    @Vassiliy-Kudryashov
    Vassiliy-Kudryashov
    on Sep 20, 2023
    Member
    More actions

    "-ea" looks irrelevant, it just enables assertions

    Reacted by Alena Lisevych
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @Vassiliy-Kudryashov
    Vassiliy-Kudryashov

Labels

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

Type

No type

Projects

  • UTBot Java
    Status
    Todo
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.