diff --git a/src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java new file mode 100644 index 000000000..0ffbf4365 --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/assertj/TestNgToAssertJTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.testing.assertj; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class TestNgToAssertJTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "testng")) + .recipeFromResources("org.openrewrite.java.testing.assertj.Assertj"); + } + + /** + * Verify that we can throw new Exception from the JavaTemplate in the generated recipe. + */ + @DocumentExample + @Test + void failWithMessage() { + rewriteRun( + //language=java + java( + """ + import static org.testng.Assert.fail; + + class Test { + void test() { + fail("foo"); + fail("foo", new IllegalStateException()); + fail(); + } + } + """, + """ + import static org.assertj.core.api.Assertions.fail; + + class Test { + void test() { + fail("foo"); + fail("foo", new IllegalStateException()); + throw new AssertionError(); + } + } + """ + ) + ); + } + + /** + * Verify some assertions as implemented through Refaster rules converted to Recipes. No need to test all variants. + */ + @Test + void assertTrueFalse() { + rewriteRun( + //language=java + java( + """ + import static org.testng.Assert.assertFalse; + import static org.testng.Assert.assertTrue; + + class Test { + void test() { + assertTrue(true); + assertTrue(true, "foo"); + assertFalse(false); + assertFalse(false, "foo"); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + + class Test { + void test() { + assertThat(true).isTrue(); + assertThat(true).withFailMessage("foo").isTrue(); + assertThat(false).isFalse(); + assertThat(false).withFailMessage("foo").isFalse(); + } + } + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java deleted file mode 100644 index 136b8c23c..000000000 --- a/src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.java.testing.testng; - -import org.junit.jupiter.api.Test; -import org.openrewrite.DocumentExample; -import org.openrewrite.InMemoryExecutionContext; -import org.openrewrite.java.JavaParser; -import org.openrewrite.test.RecipeSpec; -import org.openrewrite.test.RewriteTest; - -import static org.openrewrite.java.Assertions.java; - -class TestNgToAssertJTest implements RewriteTest { - @Override - public void defaults(RecipeSpec spec) { - spec - .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),"testng")) - .recipeFromResources("org.openrewrite.java.testing.testng.TestNgToAssertj"); - } - - @DocumentExample - @Test - void fail() { - rewriteRun( - //language=java - java( - """ - import static org.testng.Assert.fail; - - class Test { - void testFail() { - fail("foo"); - fail("foo", new IllegalStateException()); - } - } - """, - """ - import org.assertj.core.api.Assertions; - - class Test { - void testFail() { - Assertions.fail("foo"); - Assertions.fail("foo", new IllegalStateException()); - } - } - """ - ) - ); - } -}