From 5c49f67c3f8410839941501fd28f656b9d0a5f4f Mon Sep 17 00:00:00 2001 From: martinfrancois Date: Tue, 11 Aug 2026 19:56:27 +0200 Subject: [PATCH] MockUtilsToStatic: add failing tests for cross-file field use and case-label declaration doNotRemoveFieldUsedInAnotherClass pins the unsound single-file use analysis: a MockUtil field referenced only from another source file is removed, so that file stops compiling. mockUtilsVariableInSwitchCaseToStatic pins the declaration left behind under a case label after its uses are migrated. Both are marked @Disabled as known-failing. --- .../mockito/MockUtilsToStaticTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/test/java/org/openrewrite/java/testing/mockito/MockUtilsToStaticTest.java b/src/test/java/org/openrewrite/java/testing/mockito/MockUtilsToStaticTest.java index dbe746cc3..1b166f84d 100644 --- a/src/test/java/org/openrewrite/java/testing/mockito/MockUtilsToStaticTest.java +++ b/src/test/java/org/openrewrite/java/testing/mockito/MockUtilsToStaticTest.java @@ -15,6 +15,7 @@ */ package org.openrewrite.java.testing.mockito; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; @@ -98,6 +99,79 @@ public void isMockExample() { ); } + @Disabled("The single-file use analysis removes a MockUtil field still referenced from another source file, so that file no longer compiles") + @Test + void doNotRemoveFieldUsedInAnotherClass() { + //language=java + rewriteRun( + java( + """ + package mockito.example; + + import org.mockito.internal.util.MockUtil; + + public class MockitoMockUtils { + public MockUtil util = new MockUtil(); + } + """ + ), + java( + """ + package mockito.example; + + public class MockitoMockUtilsUser { + public boolean isMockUtilSet(MockitoMockUtils utils) { + return utils.util != null; + } + } + """ + ) + ); + } + + @Disabled("A fully migrated MockUtil declaration directly under a case label is not removed because its parent is the case, not a block") + @Test + void mockUtilsVariableInSwitchCaseToStatic() { + //language=java + rewriteRun( + java( + """ + package mockito.example; + + import org.mockito.internal.util.MockUtil; + + public class MockitoMockUtils { + public boolean isMockExample(int mode, Object value) { + switch (mode) { + case 1: + MockUtil util = new MockUtil(); + return util.isMock(value); + default: + return false; + } + } + } + """, + """ + package mockito.example; + + import org.mockito.internal.util.MockUtil; + + public class MockitoMockUtils { + public boolean isMockExample(int mode, Object value) { + switch (mode) { + case 1: + return MockUtil.isMock(value); + default: + return false; + } + } + } + """ + ) + ); + } + @Test void mockUtilsFieldToStatic() { //language=java