From a1543186ca385c053f8104c588513b21b860c060 Mon Sep 17 00:00:00 2001 From: martinfrancois Date: Tue, 11 Aug 2026 20:15:12 +0200 Subject: [PATCH] AssertTrueInstanceofToAssertInstanceOf: add failing tests for shadowed call and dropped receiver qualifyWhenAssertInstanceOfDeclaredInClass pins that the migrated unqualified call must not bind to an assertInstanceOf declared in the class (JLS 6.5.7.1). noChangeWhenInstanceReceiverHasSideEffect pins that an instance receiver such as getAssertions() must not be dropped, since its side effects are lost. Both are marked known-failing with @Disabled. --- ...tTrueInstanceofToAssertInstanceOfTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/test/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOfTest.java b/src/test/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOfTest.java index e4d26a2af..cbcd11fc0 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOfTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AssertTrueInstanceofToAssertInstanceOfTest.java @@ -15,6 +15,7 @@ */ package org.openrewrite.java.testing.junit5; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; @@ -262,4 +263,72 @@ void test() { """ )); } + + @Disabled("The migrated unqualified call binds to an assertInstanceOf declared in the class instead of JUnit's, per JLS 6.5.7.1") + @Test + void qualifyWhenAssertInstanceOfDeclaredInClass() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.Test; + + import static org.junit.jupiter.api.Assertions.assertTrue; + + class ATest { + static void assertInstanceOf(Class type, Object value) { + throw new AssertionError("wrong owner"); + } + + @Test + void test() { + Object obj = "example"; + assertTrue(obj instanceof String); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + import org.junit.jupiter.api.Test; + + class ATest { + static void assertInstanceOf(Class type, Object value) { + throw new AssertionError("wrong owner"); + } + + @Test + void test() { + Object obj = "example"; + Assertions.assertInstanceOf(String.class, obj); + } + } + """ + )); + } + + @Disabled("The recipe drops the getAssertions() receiver expression, so its side effects are lost from the migrated code") + @Test + void noChangeWhenInstanceReceiverHasSideEffect() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.Assertions; + import org.junit.jupiter.api.Test; + + class ATest { + Assertions getAssertions() { + System.out.println("side effect"); + return null; + } + + @Test + void test() { + Object obj = "example"; + getAssertions().assertTrue(obj instanceof String); + } + } + """ + )); + } }