Skip to content

MockitoWhenOnStaticToMockStatic fails when using unnamed var (_) notation #1051

Description

@tschut

I am using

  • rewrite-testing-frameworks 3.41.0

The MockitoWhenOnStaticToMockStatic creates invalid code when _ is used. Consider the following example:

public class MockitoWhenStaticTest implements RewriteTest {
    @Override
    public void defaults(RecipeSpec spec) {
        spec
            .recipe(new MockitoWhenOnStaticToMockStatic())
            .parser(Java25Parser.builder()
                .classpathFromResources(
                    new InMemoryExecutionContext(),
                    "junit-4",
                    "junit-jupiter-api-5",
                    "mockito-core-3.12",
                    "mockito-junit-jupiter-3.12",
                    "testng"
                )
                .dependsOn("""
                    package org.example;
                    public class A {
                        public static Integer getNumber() {
                            return 42;
                        }
                    }
                    """));
    }

    @Test
    public void test() {
        rewriteRun(
            //language=java
            java(
                """
                    import org.example.A;
                    import org.mockito.Mockito;
                    import org.mockito.MockedStatic;
                    import static org.junit.Assert.assertEquals;
                    import static org.mockito.Mockito.when;

                    class Test {
                        void test() {
                            try (var _ = Mockito.mockStatic(A.class)) {
                                when(A.getNumber()).thenReturn(-1);
                                assertEquals(A.getNumber(), -1);
                            }
                        }
                    }
                    """,
                // NOTE: This is incorrect
                """
                    import org.example.A;
                    import org.mockito.Mockito;
                    import org.mockito.MockedStatic;
                    import static org.junit.Assert.assertEquals;
                    import static org.mockito.Mockito.when;

                    class Test {
                        void test() {
                            try (var _ = Mockito.mockStatic(A.class)) {
                                _.when(() -> A.getNumber()).thenReturn(-1);  // <-- doesn't compile
                                assertEquals(A.getNumber(), -1);
                            }
                        }
                    }
                    """
            )
        );
    }
}

To get to working code, the recipe could either skip cases like these, or try to decide on a sensible variable name instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions