Skip to content

JDK17: UtBot tries to mock unexported packages and creates uncompilable tests #2070

Open
@tyuldashev

Description

@tyuldashev

Description

On sample code which works with network UtBot tries to create mockStatic(sun.net.util.IPAddressUtil.class) while IPAddressUtill is not exported from JDK module, so resulting tests are not compilable.

To Reproduce

Steps to reproduce the behavior:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
...
    public int connect(Socket socket) throws IOException {
        socket.connect(new InetSocketAddress("0.0.0.0", 22));
        return 0;
    }
  1. Set JDK 17 as project JDK
  2. Invoke test generation for code sample above, use "Mock everything outside the package" option.
  3. Open generated tests

Expected behavior

Generated test are compilable.

Actual behavior

Generated test is not compilable, because uses internal JDK class

    @Test(description = "connect: -> return 0")
    public void testConnect_ReturnZero() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException {
        org.mockito.MockedStatic mockedStatic = null;
        try {
            mockedStatic = mockStatic(sun.net.util.IPAddressUtil.class);
            (mockedStatic.when(() -> sun.net.util.IPAddressUtil.digit(anyChar(), anyInt()))).thenReturn(0);
            byte[] byteArray = new byte[16];
            (mockedStatic.when(() -> sun.net.util.IPAddressUtil.validateNumericFormatV4(any()))).thenReturn(byteArray);
...

Visual proofs (screenshots, logs, images)
image

Additional context

It seems we have two options to resolve that problem:

  • Detect somehow that class is not exported and don't generate such tests.
  • Generate test as is but add some comment that user has to add those --add-exports manually to test run configuration, see the same suggestion for --add-opens. And update the docs as well - #2047

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