diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/SimplifyTestThrows.java b/src/main/java/org/openrewrite/java/testing/cleanup/SimplifyTestThrows.java new file mode 100644 index 000000000..2c97e26bf --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/cleanup/SimplifyTestThrows.java @@ -0,0 +1,116 @@ +/* + * 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.cleanup;
+
+import org.openrewrite.*;
+import org.openrewrite.internal.lang.Nullable;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.search.UsesType;
+import org.openrewrite.java.tree.*;
+import org.openrewrite.marker.Markers;
+
+import java.time.Duration;
+import java.util.List;
+
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
+
+public class SimplifyTestThrows extends Recipe {
+
+ private static final String FQN_JAVA_LANG_EXCEPTION = "java.lang.Exception";
+
+ @Override
+ public String getDisplayName() {
+ return "Simplify `throws` statements of tests";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Replace all thrown exception classes of test method signatures by `Exception`.";
+ }
+
+ @Override
+ public Duration getEstimatedEffortPerOccurrence() {
+ return Duration.ofMinutes(1);
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return Preconditions.check(
+ Preconditions.or(
+ new UsesType<>("org.junit.jupiter.api.Test", false),
+ new UsesType<>("org.junit.jupiter.api.TestTemplate", false),
+ new UsesType<>("org.junit.jupiter.api.RepeatedTest", false),
+ new UsesType<>("org.junit.jupiter.params.ParameterizedTest", false),
+ new UsesType<>("org.junit.jupiter.api.TestFactory", false)
+ ),
+ new JavaIsoVisitor
+ * 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.cleanup;
+
+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 SimplifyTestThrowsTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),
+ "junit-jupiter-api-5.9", "junit-jupiter-params-5.9"))
+ .recipe(new SimplifyTestThrows());
+ }
+
+ @DocumentExample
+ @Test
+ void simplifyExceptions() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import java.io.IOException;
+ import org.junit.jupiter.api.Test;
+
+ class ATest {
+ @Test
+ void throwsMultiple() throws IOException, ArrayIndexOutOfBoundsException, ClassCastException {
+ }
+ }
+ """,
+ """
+ import org.junit.jupiter.api.Test;
+
+ class ATest {
+ @Test
+ void throwsMultiple() throws Exception {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void normalMethodNoChanges() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import java.io.IOException;
+ import org.junit.jupiter.api.Test;
+
+ class ATest {
+ void noTest() throws IOException {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void noThrowsDeclaration() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+
+ class ATest {
+ @Test
+ void noThrows() {
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void usesGeneralExceptionAlready() {
+ //language=java
+ rewriteRun(
+ java(
+ """
+ import org.junit.jupiter.api.Test;
+
+ class ATest {
+ @Test
+ void throwsEx() throws Exception {
+ }
+ }
+ """
+ )
+ );
+ }
+}