openrewrite / rewrite-testing-frameworks

OpenRewrite recipes that perform common Java testing migration tasks.
Apache License 2.0
68 stars 57 forks source link

UpdateBeforeAfterAnnotations does not check for existing target annotations #501

Open timo-abele opened 2 months ago

timo-abele commented 2 months ago

What version of OpenRewrite are you using?

I was using the latest version as of 2024-02-14, I bet this hasn't been fixed in the meantime because it is an edge case.

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

What is the problem?

A test base class uses both JUnit 4 and Jupiter, as a consequence its setup method is annotated with both @Before and @BeforeEach. The recipe will now replace the old @Before with the already present @BeforeEach annotation causing

Duplicate annotation. The declaration of 'org.junit.jupiter.api.BeforeEach' does not have a valid java.lang.annotation.Repeatable annotation

...
class TestBase {
    @Before
    @BeforeEach
    void setup() {
      //...
    }
}

While rare this issue could pop up in all kinds of unexpected places, so it might be worth introducing something like MaybeChangeType that considers the already present annotations.

What is the full stack trace of any errors you encountered?

see above

Are you interested in contributing a fix to OpenRewrite?

No

timtebeek commented 1 month ago

Oh wow, hadn't ever seen that pattern indeed. It'll be caused by us changing the type of the existing Before annotations to the new BeforeEach annotations. https://github.com/openrewrite/rewrite-testing-frameworks/blob/41a01dac25eb80d4e79750856c800b72a96868a1/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java#L55-L58

If we would want to support this we'd have to revise that logic to also look for existing target annotations and remove those. That's sure possible, but I'd expect that to be rare and perhaps then not immediately worth the trouble for a one time migration. Would you agree there?