openrewrite / rewrite-testing-frameworks

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

Adopt AssertJ duration assertions #400

Open timtebeek opened 1 year ago

timtebeek commented 1 year ago

What problem are you trying to solve?

More expressive assertions around durations, using the methods defined in: https://www.javadoc.io/static/org.assertj/assertj-core/3.24.2/org/assertj/core/api/DurationAssert.html

What precondition(s) should be checked before applying this recipe?

Durations are used in the class.

Describe the situation before applying the recipe

assertThat(Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds()).isEqualTo(1);

Describe the situation after applying the recipe

assertThat(Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt())).hasSeconds(1);

Any additional context

We might also want to simplify the durations of the resulting assertions, similar to what we already do in: https://docs.openrewrite.org/recipes/staticanalysis/simplifydurationcreationunits

-.hasSeconds(300)
+.hasMinutes(5)
timtebeek commented 1 year ago

Once merged we can run this against spring-security, as seen previously in https://github.com/spring-projects/spring-security/pull/13619

timtebeek commented 1 year ago

Seeing some errors when run against Spring Security; would you mind investigating @AlekSimpson ?

timtebeek commented 1 year ago

Error:

assertThat(returned).as("The returned value is not correct.").isEqualTo(Tag.EVAL_PAGE)

Message:

class org.openrewrite.java.tree.J$Identifier cannot be cast to class org.openrewrite.java.tree.J$MethodInvocation (org.openrewrite.java.tree.J$Identifier and org.openrewrite.java.tree.J$MethodInvocation are in unnamed module of loader 'app')

Detail:

java.lang.ClassCastException: class org.openrewrite.java.tree.J$Identifier cannot be cast to class org.openrewrite.java.tree.J$MethodInvocation (org.openrewrite.java.tree.J$Identifier and org.openrewrite.java.tree.J$MethodInvocation are in unnamed module of loader 'app')
org.openrewrite.java.testing.assertj.AdoptAssertJDurationAssertions$AdoptAssertJDurationAssertionsVisitor.simplifyMultipleAssertions(AdoptAssertJDurationAssertions.java:132)
org.openrewrite.java.testing.assertj.AdoptAssertJDurationAssertions$AdoptAssertJDurationAssertionsVisitor.visitMethodInvocation(AdoptAssertJDurationAssertions.java:110)
org.openrewrite.java.testing.assertj.AdoptAssertJDurationAssertions$AdoptAssertJDurationAssertionsVisitor.visitMethodInvocation(AdoptAssertJDurationAssertions.java:73)
org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3723)
org.openrewrite.java.tree.J.accept(J.java:59)
org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1299)
...
timtebeek commented 1 year ago

Error:

assertThat(chain.length).as("Unexpected chain size").isEqualTo(1)

Message:

class org.openrewrite.java.tree.J$FieldAccess cannot be cast to class org.openrewrite.java.tree.J$MethodInvocation (org.openrewrite.java.tree.J$FieldAccess and org.openrewrite.java.tree.J$MethodInvocation are in unnamed module of loader 'app')

Detail:

java.lang.ClassCastException: class org.openrewrite.java.tree.J$FieldAccess cannot be cast to class org.openrewrite.java.tree.J$MethodInvocation (org.openrewrite.java.tree.J$FieldAccess and org.openrewrite.java.tree.J$MethodInvocation are in unnamed module of loader 'app')
org.openrewrite.java.testing.assertj.AdoptAssertJDurationAssertions$AdoptAssertJDurationAssertionsVisitor.simplifyMultipleAssertions(AdoptAssertJDurationAssertions.java:132)
org.openrewrite.java.testing.assertj.AdoptAssertJDurationAssertions$AdoptAssertJDurationAssertionsVisitor.visitMethodInvocation(AdoptAssertJDurationAssertions.java:110)
org.openrewrite.java.testing.assertj.AdoptAssertJDurationAssertions$AdoptAssertJDurationAssertionsVisitor.visitMethodInvocation(AdoptAssertJDurationAssertions.java:73)
org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3723)
org.openrewrite.java.tree.J.accept(J.java:59)
org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1299)
...
timtebeek commented 1 year ago

Now the recipes don't seem to make any changes on spring-security, although I'm fairly certain there's some improvements to be made still.