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.
I am using
The
MockitoWhenOnStaticToMockStaticcreates invalid code when_is used. Consider the following example:To get to working code, the recipe could either skip cases like these, or try to decide on a sensible variable name instead.