rewrite-testing-frameworks icon indicating copy to clipboard operation
rewrite-testing-frameworks copied to clipboard

`RemoveTestPrefix` should not (only) rename methods called from other methods

Open timtebeek opened this issue 2 years ago • 4 comments

Quickly noted down here, can be tackled individually.

  • [x] Should not rename test when there's a static method import of the same name (can lead to conflicts)
  • [ ] Should not (only) rename test method when called from other methods (which unfortunately happens)

timtebeek avatar Aug 31 '22 15:08 timtebeek

Note: this issue is a bit more visible now that RemoveTestPrefix is included in JUnit5BestPractices: https://github.com/openrewrite/rewrite-testing-frameworks/commit/cb59832d19f6e44d6a9c5bc5e9f44860b377c3ad

nmck257 avatar Jan 04 '24 21:01 nmck257

Just to offer up a test case for the first issue listed above as we ran into this in our codebase:

  @Test
  void testRemoveTestPrefixWithClashingMethod() {
    rewriteRun(
        spec -> spec.recipe(new RemoveTestPrefix()),
        java(
            """
            package com.spotify.helloworld;

            import org.junit.jupiter.api.Test;
            import static java.util.List.of;

            public class FooTest {

                @Test
                void testOf() {
                  of();
                }
            }"""));
  }

I can put this test in a PR if that is preferable. Not sure I'd know how to fix it though.

protocol7 avatar Jul 03 '24 12:07 protocol7

Thanks @protocol7 ; this one was easy it seems;

  • Fixed in https://github.com/openrewrite/rewrite-testing-frameworks/pull/543

timtebeek avatar Jul 03 '24 13:07 timtebeek

We could maybe fix the remaining issue (within a single class) by switching to ChangeMethodName in a doAfterVisit instead of using the .withXyz() methods in this recipe directly. But we'd need to correctly construct the method pattern to match from the method declaration to do so, and even then it would only work in a single compilation unit, not across.

timtebeek avatar Jul 03 '24 16:07 timtebeek