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

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

Open
tyuldashev opened this issue Mar 29, 2023 · 0 comments
Assignees
Labels
comp-codegen Issue is related to code generator ctg-bug Issue is a bug

Comments

@tyuldashev
Copy link
Collaborator

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
@tyuldashev tyuldashev added ctg-bug Issue is a bug comp-codegen Issue is related to code generator labels Mar 29, 2023
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