From 964e677739350ce16cc0b4ee55fb47d152a2baf0 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 7 Mar 2024 23:27:31 +0100 Subject: [PATCH] Fix JUnit assertion argument order before AssertJ migration Fixes https://github.com/openrewrite/rewrite-testing-frameworks/issues/492 --- .../resources/META-INF/rewrite/assertj.yml | 2 + .../testing/junit5/CleanupAssertionsTest.java | 44 +++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index f60e3cc07..18cac7ae6 100644 --- a/src/main/resources/META-INF/rewrite/assertj.yml +++ b/src/main/resources/META-INF/rewrite/assertj.yml @@ -346,6 +346,8 @@ tags: - testing - assertj recipeList: + # First improve the assertions for JUnit, to fix inverted expected/actual values + - org.openrewrite.java.testing.junit5.CleanupAssertions - org.openrewrite.java.testing.assertj.JUnitAssertArrayEqualsToAssertThat - org.openrewrite.java.testing.assertj.JUnitAssertEqualsToAssertThat - org.openrewrite.java.testing.assertj.JUnitAssertFalseToAssertThat diff --git a/src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java index 45efe2db4..0553fe906 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/CleanupAssertionsTest.java @@ -32,10 +32,7 @@ class CleanupAssertionsTest implements RewriteTest { public void defaults(RecipeSpec spec) { spec .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9")) - .recipe(Environment.builder() - .scanRuntimeClasspath("org.openrewrite.java.testing.junit5") - .build() - .activateRecipes("org.openrewrite.java.testing.junit5.CleanupAssertions")); + .recipeFromResources("org.openrewrite.java.testing.junit5.CleanupAssertions"); } @DocumentExample @@ -130,4 +127,43 @@ void test() { ) ); } + + @Test + void assertNotEqualsInverted() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.Assertions; + import org.junit.jupiter.api.Test; + + class A { + class B {} + + @Test + public void FlippedParams(){ + B myVariable = new B(); + + Assertions.assertNotEquals(myVariable, null); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + import org.junit.jupiter.api.Test; + + class A { + class B {} + + @Test + public void FlippedParams(){ + B myVariable = new B(); + + Assertions.assertNotEquals(null, myVariable); + } + } + """ + ) + ); + } }