openrewrite / rewrite-testing-frameworks

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

Assertj removes asLocale #496

Open timo-abele opened 7 months ago

timo-abele commented 7 months ago

How are you running OpenRewrite?

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

    <profile>
        <id>openrewrite</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.openrewrite.maven</groupId>
                    <artifactId>rewrite-maven-plugin</artifactId>
                    <version>5.23.1</version>
                    <configuration>
                        <activeRecipes>
                            <recipe>org.openrewrite.java.testing.assertj.Assertj</recipe>
                        </activeRecipes>
                        <failOnDryRunResults>true</failOnDryRunResults>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.openrewrite.recipe</groupId>
                            <artifactId>rewrite-testing-frameworks</artifactId>
                            <version>2.4.1</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>

What is the smallest, simplest way to reproduce the problem?

No guarantee on completeness of imports 😄

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertTrue;

class A {
    class MyLang { MyLang(String l){} Locale asLocale(){return null;}}

    @Test
    void regressionAsLocaleShouldStay() {
        MyLang underTest = new MyLang("he");

        Set<Locale> localeSet = new HashSet<>();

        assertTrue(localeSet.contains(underTest.asLocale()));
    }
}

What did you expect to see?

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

class A {
    class MyLang { MyLang(String l){} Locale asLocale(){return null;}}

    @Test
    void regressionAsLocaleShouldStay() {
        MyLang underTest = new MyLang("he");

        Set<Locale> localeSet = new HashSet<>();

        assertThat(localeSet).contains(underTest.asLocale());
    }
}

What did you see instead?

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

class A {
    class MyLang { MyLang(String l){} Locale asLocale(){return null;}}

    @Test
    void regressionAsLocaleShouldStay() {
        MyLang underTest = new MyLang("he");

        Set<Locale> localeSet = new HashSet<>();

        assertThat(localeSet).contains(underTest);
    }
}

Comment

In a nutshell: the asLocale call on my custom class is erroneously dropped. I suppose the asLocale can be skipped for some jdk class but that the recipe does not check for this type

timtebeek commented 7 months ago

That's quite unexpected, thanks for the clear report! Definitely something to fix.