Skip to content

Commit 144f337

Browse files
OpenRewrite recipe best practices
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices?organizationId=QUxML01vZGVybmUvTW9kZXJuZSArIE9wZW5SZXdyaXRl Co-authored-by: Moderne <team@moderne.io>
1 parent c33bba2 commit 144f337

5 files changed

Lines changed: 156 additions & 9 deletions

File tree

src/main/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoid.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
7373
}
7474

7575
// Remove return statements that are not in nested classes or lambdas
76-
return m.withBody((J.Block) new RemoveDirectReturns().visitBlock(requireNonNull(m.getBody()), ctx));
76+
return m.withBody((J.Block) new RemoveDirectReturns().visit(requireNonNull(m.getBody()), ctx, getCursor().getParentTreeCursor()));
7777
}
7878
});
7979
}

src/main/java/org/openrewrite/java/testing/mockito/RemoveInitMocksIfRunnersSpecified.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
143143
@Override
144144
public J.@Nullable If visitIf(J.If iff, ExecutionContext ctx) {
145145
J.If i = super.visitIf(iff, ctx);
146-
if (i != iff
147-
&& i.getThenPart() instanceof J.Block
148-
&& ((J.Block) i.getThenPart()).getStatements().isEmpty()
149-
&& i.getElsePart() == null) {
146+
if (i != iff &&
147+
i.getThenPart() instanceof J.Block &&
148+
((J.Block) i.getThenPart()).getStatements().isEmpty() &&
149+
i.getElsePart() == null) {
150150
return null;
151151
}
152152
return i;

src/main/java/org/openrewrite/java/testing/mockito/ThenThrowCheckedExceptionToRuntimeException.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openrewrite.Preconditions;
2121
import org.openrewrite.Recipe;
2222
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.internal.ListUtils;
2324
import org.openrewrite.java.JavaIsoVisitor;
2425
import org.openrewrite.java.MethodMatcher;
2526
import org.openrewrite.java.search.UsesType;
@@ -28,8 +29,6 @@
2829
import org.openrewrite.java.tree.JavaType;
2930
import org.openrewrite.java.tree.TypeUtils;
3031

31-
import org.openrewrite.internal.ListUtils;
32-
3332

3433
public class ThenThrowCheckedExceptionToRuntimeException extends Recipe {
3534
@Getter

src/main/resources/META-INF/rewrite/examples.yml

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,29 @@ examples:
12031203
language: java
12041204
---
12051205
type: specs.openrewrite.org/v1beta/example
1206+
recipeName: org.openrewrite.java.testing.cleanup.AssertEqualsIntegralDeltaToAssertEquals
1207+
examples:
1208+
- description: '`AssertEqualsIntegralDeltaToAssertEqualsTest#removeIntDelta`'
1209+
sources:
1210+
- before: |
1211+
import static org.junit.jupiter.api.Assertions.assertEquals;
1212+
1213+
public class Test {
1214+
void test() {
1215+
assertEquals(1, 2, 0);
1216+
}
1217+
}
1218+
after: |
1219+
import static org.junit.jupiter.api.Assertions.assertEquals;
1220+
1221+
public class Test {
1222+
void test() {
1223+
assertEquals(1, 2);
1224+
}
1225+
}
1226+
language: java
1227+
---
1228+
type: specs.openrewrite.org/v1beta/example
12061229
recipeName: org.openrewrite.java.testing.cleanup.AssertLiteralBooleanRemovedRecipe
12071230
examples:
12081231
- description: '`AssertLiteralBooleanRemovedTest#assertWithStaticImports`'
@@ -4722,6 +4745,43 @@ examples:
47224745
language: java
47234746
---
47244747
type: specs.openrewrite.org/v1beta/example
4748+
recipeName: org.openrewrite.java.testing.mockito.PowerMockitoDoStubbingToMockito
4749+
examples:
4750+
- description: '`PowerMockitoDoStubbingToMockitoTest#doNothingWithStringMethodName`'
4751+
sources:
4752+
- before: |
4753+
import org.junit.jupiter.api.BeforeEach;
4754+
import org.powermock.api.mockito.PowerMockito;
4755+
import java.util.Calendar;
4756+
4757+
class MyTest {
4758+
4759+
private Calendar calendarSpy;
4760+
4761+
@BeforeEach
4762+
void setUp() throws Exception {
4763+
calendarSpy = PowerMockito.spy(Calendar.getInstance());
4764+
PowerMockito.doNothing().when(calendarSpy, "clear");
4765+
}
4766+
}
4767+
after: |
4768+
import org.junit.jupiter.api.BeforeEach;
4769+
import org.powermock.api.mockito.PowerMockito;
4770+
import java.util.Calendar;
4771+
4772+
class MyTest {
4773+
4774+
private Calendar calendarSpy;
4775+
4776+
@BeforeEach
4777+
void setUp() throws Exception {
4778+
calendarSpy = PowerMockito.spy(Calendar.getInstance());
4779+
PowerMockito.doNothing().when(calendarSpy).clear();
4780+
}
4781+
}
4782+
language: java
4783+
---
4784+
type: specs.openrewrite.org/v1beta/example
47254785
recipeName: org.openrewrite.java.testing.mockito.PowerMockitoMockStaticToMockito
47264786
examples:
47274787
- description: '`PowerMockitoMockStaticToMockitoTest#prepareForTestAnnotationIsReplacedBySingleField`'
@@ -4773,6 +4833,53 @@ examples:
47734833
language: java
47744834
---
47754835
type: specs.openrewrite.org/v1beta/example
4836+
recipeName: org.openrewrite.java.testing.mockito.RemoveDoNothingForDefaultMocks
4837+
examples:
4838+
- description: '`RemoveDoNothingForDefaultMocksTest#removesDoNothingOnMockVoidMethod`'
4839+
sources:
4840+
- before: |
4841+
import org.junit.Test;
4842+
import org.junit.runner.RunWith;
4843+
import org.mockito.Mock;
4844+
import org.mockito.junit.MockitoJUnitRunner;
4845+
import java.io.BufferedWriter;
4846+
import java.io.IOException;
4847+
4848+
import static org.mockito.Mockito.doNothing;
4849+
import static org.mockito.ArgumentMatchers.anyString;
4850+
4851+
@RunWith(MockitoJUnitRunner.class)
4852+
class MyTest {
4853+
@Mock
4854+
private BufferedWriter bufferedWriter;
4855+
4856+
@Test
4857+
public void test() throws IOException {
4858+
doNothing().when(bufferedWriter).write(anyString());
4859+
}
4860+
}
4861+
after: |
4862+
import org.junit.Test;
4863+
import org.junit.runner.RunWith;
4864+
import org.mockito.Mock;
4865+
import org.mockito.junit.MockitoJUnitRunner;
4866+
import java.io.BufferedWriter;
4867+
import java.io.IOException;
4868+
4869+
import static org.mockito.ArgumentMatchers.anyString;
4870+
4871+
@RunWith(MockitoJUnitRunner.class)
4872+
class MyTest {
4873+
@Mock
4874+
private BufferedWriter bufferedWriter;
4875+
4876+
@Test
4877+
public void test() throws IOException {
4878+
}
4879+
}
4880+
language: java
4881+
---
4882+
type: specs.openrewrite.org/v1beta/example
47764883
recipeName: org.openrewrite.java.testing.mockito.RemoveInitMocksIfRunnersSpecified
47774884
examples:
47784885
- description: '`RemoveInitMocksIfRunnersSpecifiedTest#removeInitMocksInJUnit5`'
@@ -4807,6 +4914,24 @@ examples:
48074914
language: java
48084915
---
48094916
type: specs.openrewrite.org/v1beta/example
4917+
recipeName: org.openrewrite.java.testing.mockito.RemovePowerMockClassExtensions
4918+
examples:
4919+
- description: '`RemovePowerMockClassExtensionsTest#extensionOfPowerMockTestCaseGetsRemoved`'
4920+
sources:
4921+
- before: |
4922+
import org.powermock.modules.testng.PowerMockTestCase;
4923+
4924+
public class MyPowerMockTestCase extends PowerMockTestCase {}
4925+
after: |
4926+
public class MyPowerMockTestCase {}
4927+
language: java
4928+
- before: |
4929+
package org.powermock.modules.testng;
4930+
4931+
public class PowerMockTestCase {}
4932+
language: java
4933+
---
4934+
type: specs.openrewrite.org/v1beta/example
48104935
recipeName: org.openrewrite.java.testing.mockito.RemoveTimesZeroAndOne
48114936
examples:
48124937
- description: '`RemoveTimesZeroAndOneTest#replaceTimesZero`'
@@ -5029,6 +5154,29 @@ examples:
50295154
language: java
50305155
---
50315156
type: specs.openrewrite.org/v1beta/example
5157+
recipeName: org.openrewrite.java.testing.mockito.ThenThrowCheckedExceptionToRuntimeException
5158+
examples:
5159+
- description: '`ThenThrowCheckedExceptionToRuntimeExceptionTest#replacesUndeclaredCheckedExceptionWithRuntimeException`'
5160+
sources:
5161+
- before: |
5162+
import static org.mockito.Mockito.when;
5163+
5164+
class MyTest {
5165+
void test(MyService service) {
5166+
when(service.execute("a", "b")).thenThrow(Exception.class);
5167+
}
5168+
}
5169+
after: |
5170+
import static org.mockito.Mockito.when;
5171+
5172+
class MyTest {
5173+
void test(MyService service) {
5174+
when(service.execute("a", "b")).thenThrow(RuntimeException.class);
5175+
}
5176+
}
5177+
language: java
5178+
---
5179+
type: specs.openrewrite.org/v1beta/example
50325180
recipeName: org.openrewrite.java.testing.mockito.VerifyZeroToNoMoreInteractions
50335181
examples:
50345182
- description: '`VerifyZeroToNoMoreInteractionsTest#shouldReplaceToNoMoreInteractions`'

src/test/java/org/openrewrite/java/testing/testcontainers/TestcontainersBestPracticesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.regex.Matcher;
2727
import java.util.regex.Pattern;
2828

29-
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.assertj.core.api.Assertions.assertThat;
3030
import static org.openrewrite.java.Assertions.java;
3131
import static org.openrewrite.maven.Assertions.pomXml;
3232

@@ -138,7 +138,7 @@ void dependencyUpdate() {
138138
""",
139139
spec -> spec.after(after -> {
140140
Matcher matcher = Pattern.compile("<version>(2\\.\\d+\\.\\d+)</version>").matcher(after);
141-
assertTrue(matcher.find());
141+
assertThat(matcher.find()).isTrue();
142142
String afterVersion = matcher.group(1);
143143
//language=xml
144144
return """

0 commit comments

Comments
 (0)