diff --git a/build.gradle.kts b/build.gradle.kts index 51cef6770..b15b500dd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -33,6 +33,7 @@ recipeDependencies { val rewriteVersion = rewriteRecipe.rewriteVersion.get() dependencies { implementation("org.openrewrite:rewrite-java:$rewriteVersion") + implementation("org.openrewrite:rewrite-gradle:$rewriteVersion") implementation("org.openrewrite:rewrite-maven:$rewriteVersion") runtimeOnly("org.openrewrite:rewrite-java-17:$rewriteVersion") diff --git a/src/main/java/org/openrewrite/java/testing/mockito/AnyStringToNullable.java b/src/main/java/org/openrewrite/java/testing/mockito/AnyStringToNullable.java new file mode 100644 index 000000000..2d8262169 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/mockito/AnyStringToNullable.java @@ -0,0 +1,85 @@ +/* + * Copyright 2021 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.mockito;
+
+import org.openrewrite.*;
+import org.openrewrite.internal.lang.Nullable;
+import org.openrewrite.java.*;
+import org.openrewrite.java.search.UsesMethod;
+import org.openrewrite.java.tree.J;
+
+import java.time.Duration;
+
+/**
+ * Replace Mockito 1.x `anyString()` with `nullable(String.class)`
+ */
+public class AnyStringToNullable extends Recipe {
+ private static final MethodMatcher ANY_STRING = new MethodMatcher("org.mockito.Mockito anyString()");
+ private static final String MOCKITO_CLASS_PATH = "mockito-core-3.12.4";
+ private static J.MethodInvocation nullableStringMethodTemplate = null;
+
+ @Override
+ public String getDisplayName() {
+ return "Replace Mockito 1.x `anyString()` with `nullable(String.class)`";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Since Mockito 2.10 `anyString()` no longer matches null values. Use `nullable(Class)` instead.";
+ }
+
+ @Override
+ public Duration getEstimatedEffortPerOccurrence() {
+ return Duration.ofMinutes(1);
+ }
+
+ @Nullable
+ @Override
+ protected TreeVisitor, ExecutionContext> getSingleSourceApplicableTest() {
+ return new UsesMethod<>(ANY_STRING);
+ }
+
+ @Override
+ protected TreeVisitor, ExecutionContext> getVisitor() {
+ return 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.mockito;
+
+import org.junit.jupiter.api.Test;
+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 AnyStringToNullableTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "mockito-core-3.12.4")
+ .logCompilationWarningsAndErrors(true))
+ .recipe(new AnyStringToNullable());
+ }
+
+ @Test
+ void replaceAnyStringWithNullableStringClass() {
+ //language=java
+ rewriteRun(
+ java("""
+ class Example {
+ String greet(String name) {
+ return "Hello " + name;
+ }
+ }
+ """),
+ java(
+ """
+ import static org.mockito.Mockito.anyString;
+ import static org.mockito.Mockito.mock;
+ import static org.mockito.Mockito.when;
+
+ class MyTest {
+ void test() {
+ Example example = mock(Example.class);
+ when(example.greet(anyString())).thenReturn("Hello world");
+ }
+ }
+ """,
+ """
+ import static org.mockito.ArgumentMatchers.nullable;
+ import static org.mockito.Mockito.mock;
+ import static org.mockito.Mockito.when;
+
+ class MyTest {
+ void test() {
+ Example example = mock(Example.class);
+ when(example.greet(nullable(String.class))).thenReturn("Hello world");
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void doNotReplaceAnyInt() {
+ //language=java
+ rewriteRun(
+ java("""
+ class Example {
+ String greet(int value) {
+ return "Hello " + value;
+ }
+ }
+ """),
+ java(
+ """
+ import static org.mockito.Mockito.mock;
+ import static org.mockito.Mockito.when;
+ import static org.mockito.Mockito.anyInt;
+
+ class MyTest {
+ void test() {
+ Example example = mock(Example.class);
+ when(example.greet(anyInt())).thenReturn("Hello 5");
+ }
+ }
+ """
+ )
+ );
+ }
+
+}
+
+
diff --git a/src/test/java/org/openrewrite/java/testing/mockito/AnyToNullableTest.java b/src/test/java/org/openrewrite/java/testing/mockito/AnyToNullableTest.java
new file mode 100755
index 000000000..4cfa10442
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/testing/mockito/AnyToNullableTest.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2021 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.mockito;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.config.Environment;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+import static org.openrewrite.maven.Assertions.pomXml;
+
+class AnyToNullableTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec
+ .parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(), "mockito-core-3.12.4")
+ .logCompilationWarningsAndErrors(true))
+ .recipe(Environment.builder()
+ .scanRuntimeClasspath("org.openrewrite.java.testing.mockito")
+ .build()
+ .activateRecipes("org.openrewrite.java.testing.mockito.AnyToNullable"));
+ }
+
+ @Test
+ void replaceAnyClassWithNullableClass() {
+ //language=java
+ rewriteRun(
+ //language=xml
+ pomXml("""
+