diff --git a/src/main/java/org/openrewrite/java/testing/assertj/AdoptAssertJDurationAssertions.java b/src/main/java/org/openrewrite/java/testing/assertj/AdoptAssertJDurationAssertions.java index 9431ce4a0..6588154e4 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/AdoptAssertJDurationAssertions.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/AdoptAssertJDurationAssertions.java @@ -146,7 +146,6 @@ private boolean isZero(Expression isEqualToArg) { private J.MethodInvocation applyTemplate(ExecutionContext ctx, J.MethodInvocation m, String template, String assertions, Object... parameters) { maybeAddImport(assertions, "assertThat"); J.MethodInvocation invocation = JavaTemplate.builder(template) - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3")) .staticImports(assertions + ".assertThat") .build() diff --git a/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertJInstanceOfAssertion.java b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertJInstanceOfAssertion.java index ca74b59a1..56af5643b 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertJInstanceOfAssertion.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertJInstanceOfAssertion.java @@ -100,7 +100,6 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu TypedTree clazz = (TypedTree) instanceOf.getClazz(); TypedTree rawClazz = clazz instanceof J.ParameterizedType ? ((J.ParameterizedType) clazz).getClazz() : clazz; return JavaTemplate.builder("#{any()}." + dedicatedAssertion + "(#{any(java.lang.Class)})") - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3")) .build() .apply(getCursor(), mi.getCoordinates().replace(), newAssertThat, toClassLiteral(rawClazz)); diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsIntegralDeltaToAssertEquals.java b/src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsIntegralDeltaToAssertEquals.java index 2bb526357..e38ffd2da 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsIntegralDeltaToAssertEquals.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsIntegralDeltaToAssertEquals.java @@ -64,13 +64,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) JavaTemplate t; if (mi.getSelect() == null) { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .staticImports("org.junit.jupiter.api.Assertions.assertEquals") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); } else { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .imports("org.junit.jupiter.api.Assertions") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); @@ -88,13 +86,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) JavaTemplate t; if (mi.getSelect() == null) { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .staticImports("org.junit.jupiter.api.Assertions.assertEquals") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); } else { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .imports("org.junit.jupiter.api.Assertions") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseEqualsToAssertNotEquals.java b/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseEqualsToAssertNotEquals.java index ede42b262..18f0072d0 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseEqualsToAssertNotEquals.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseEqualsToAssertNotEquals.java @@ -64,13 +64,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) JavaTemplate t; if (mi.getSelect() == null) { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .staticImports("org.junit.jupiter.api.Assertions.assertNotEquals") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); } else { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .imports("org.junit.jupiter.api.Assertions") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNegationToAssertTrue.java b/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNegationToAssertTrue.java index 1243bd959..72c52a604 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNegationToAssertTrue.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNegationToAssertTrue.java @@ -65,13 +65,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu JavaTemplate t; if (mi.getSelect() == null) { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .staticImports("org.junit.jupiter.api.Assertions.assertTrue") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); } else { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .imports("org.junit.jupiter.api.Assertions") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNullToAssertNotNull.java b/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNullToAssertNotNull.java index 78675f773..8f5e3e058 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNullToAssertNotNull.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/AssertFalseNullToAssertNotNull.java @@ -68,14 +68,12 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) JavaTemplate t; if (mi.getSelect() == null) { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .staticImports("org.junit.jupiter.api.Assertions.assertNotNull") .javaParser(JavaParser.fromJavaVersion() .classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); } else { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .imports("org.junit.jupiter.api.Assertions") .javaParser(JavaParser.fromJavaVersion() .classpathFromResources(ctx, "junit-jupiter-api-5")) diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/AssertTrueEqualsToAssertEquals.java b/src/main/java/org/openrewrite/java/testing/cleanup/AssertTrueEqualsToAssertEquals.java index 9c140b369..c1ee8b523 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/AssertTrueEqualsToAssertEquals.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/AssertTrueEqualsToAssertEquals.java @@ -67,13 +67,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) JavaTemplate t; if (mi.getSelect() == null) { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .staticImports("org.junit.jupiter.api.Assertions.assertEquals") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); } else { t = JavaTemplate.builder(sb.toString()) - .contextSensitive() .imports("org.junit.jupiter.api.Assertions") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build(); diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJ.java b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJ.java index f9ae8e1ea..d2dd2244e 100644 --- a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJ.java +++ b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestIsMatcherToAssertJ.java @@ -85,7 +85,6 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu new Object[]{actualArgument, expectedArgument}; return JavaTemplate.builder(template) - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3")) .staticImports("org.assertj.core.api.Assertions.assertThat") .build() diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java index 420653457..bdf557f7d 100644 --- a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java +++ b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java @@ -106,7 +106,6 @@ private J.MethodInvocation replace(J.MethodInvocation mi, ExecutionContext ctx) (reasonArgument != null ? ".as(#{any(String)})" : "") + ".%s(%s)", actual, assertion, argsTemplate)) - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3")) .staticImports( "org.assertj.core.api.Assertions.assertThat", diff --git a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJ.java b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJ.java index 3f94b1f1a..e23c030e5 100644 --- a/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJ.java +++ b/src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestNotMatcherToAssertJ.java @@ -103,7 +103,6 @@ private J.MethodInvocation handleTwoArgumentCase(J.MethodInvocation mi, Expressi .collect(joining(", ")); JavaTemplate template = JavaTemplate.builder(String.format("assertThat(%s).%s(%s)", actual, assertion, argumentsTemplate)) - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3")) .staticImports("org.assertj.core.api.Assertions.assertThat") .build(); @@ -136,7 +135,6 @@ private J.MethodInvocation handleThreeArgumentCase(J.MethodInvocation mi, Expres .collect(joining(", ")); JavaTemplate template = JavaTemplate.builder(String.format("assertThat(%s).as(#{any(String)}).%s(%s)", actual, assertion, argumentsTemplate)) - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3")) .staticImports("org.assertj.core.api.Assertions.assertThat") .build(); diff --git a/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java b/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java index c5f184481..dd5c99ef9 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/RemoveTryCatchFailBlocks.java @@ -129,7 +129,6 @@ private J.MethodInvocation replaceWithAssertDoesNotThrowWithoutStringExpression( maybeAddImport("org.junit.jupiter.api.Assertions"); maybeRemoveCatchTypes(try_); return JavaTemplate.builder("Assertions.assertDoesNotThrow(() -> #{any()});") - .contextSensitive() .imports("org.junit.jupiter.api.Assertions") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5")) .build() diff --git a/src/main/java/org/openrewrite/java/testing/junit5/UpdateMockWebServer.java b/src/main/java/org/openrewrite/java/testing/junit5/UpdateMockWebServer.java index bc6355466..dd6fbe756 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/UpdateMockWebServer.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/UpdateMockWebServer.java @@ -95,7 +95,6 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex for (Statement statement : cd.getBody().getStatements()) { if (statement == afterEachMethod) { cd = JavaTemplate.builder("#{any(okhttp3.mockwebserver.MockWebServer)}.close();") - .contextSensitive() .imports(AFTER_EACH_FQN, MOCK_WEB_SERVER_FQN, IO_EXCEPTION_FQN) .javaParser(JavaParser.fromJavaVersion() .classpathFromResources(ctx, diff --git a/src/main/java/org/openrewrite/java/testing/mockito/DisableUnsupportedPowerMockTests.java b/src/main/java/org/openrewrite/java/testing/mockito/DisableUnsupportedPowerMockTests.java index 73fb46dd7..ccd099a6d 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/DisableUnsupportedPowerMockTests.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/DisableUnsupportedPowerMockTests.java @@ -129,7 +129,6 @@ private T disable(T target, Framework framework, ExecutionContext maybeAddImport(framework.disabledAnnotation); return JavaTemplate.builder("@" + simpleName(framework.disabledAnnotation) + "(\"PowerMock test disabled by migration: rework it not to rely on private members\")") - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, framework.classpathResource)) .imports(framework.disabledAnnotation) .build() diff --git a/src/main/java/org/openrewrite/java/testing/mockito/MockConstructionToTryWithResources.java b/src/main/java/org/openrewrite/java/testing/mockito/MockConstructionToTryWithResources.java index 8fa8c340e..e7b523f3f 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/MockConstructionToTryWithResources.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/MockConstructionToTryWithResources.java @@ -145,7 +145,6 @@ public J visitBlock(J.Block block, ExecutionContext ctx) { // Create try-with-resources J.Try tryWithResources = JavaTemplate.builder("try (#{any()}) {}") - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-5")) .build() .apply(new Cursor(getCursor(), varDecl), varDecl.getCoordinates().replace(), varDecl); diff --git a/src/main/java/org/openrewrite/java/testing/mockito/PowerMockWhiteboxSetInternalStateToInjectMocks.java b/src/main/java/org/openrewrite/java/testing/mockito/PowerMockWhiteboxSetInternalStateToInjectMocks.java index fd6214e75..01bc6fe52 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/PowerMockWhiteboxSetInternalStateToInjectMocks.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/PowerMockWhiteboxSetInternalStateToInjectMocks.java @@ -143,7 +143,6 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m J.VariableDeclarations withoutInitializer = multiVariable.withVariables( ListUtils.map(multiVariable.getVariables(), v -> v.withInitializer(null))); J.VariableDeclarations annotated = JavaTemplate.builder("@InjectMocks") - .contextSensitive() .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) .imports(INJECT_MOCKS) .build() diff --git a/src/main/java/org/openrewrite/java/testing/mockito/ScopedMocks.java b/src/main/java/org/openrewrite/java/testing/mockito/ScopedMocks.java index 27bd0c782..1d952d9ba 100644 --- a/src/main/java/org/openrewrite/java/testing/mockito/ScopedMocks.java +++ b/src/main/java/org/openrewrite/java/testing/mockito/ScopedMocks.java @@ -362,7 +362,6 @@ private static J.ClassDeclaration appendToMethodAnnotatedWith(JavaVisitor list) { ) ); } + + @Test + void arrayOfClassDeclaredInTestFile() { + rewriteRun( + //language=java + java( + """ + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(Item[] items) { + assertThat(items.length).isEqualTo(2); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(Item[] items) { + assertThat(items).hasSize(2); + } + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionTest.java index f3dcc9323..2320d5d99 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertionTest.java @@ -871,4 +871,40 @@ void testMethod(List list) { ) ); } + + @Test + void collectionOfClassDeclaredInTestFile() { + rewriteRun( + spec -> spec.recipe(new SimplifyChainedAssertJAssertion("isEmpty", "isTrue", "isEmpty", "java.util.Collection")), + //language=java + java( + """ + import java.util.List; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(List items) { + assertThat(items.isEmpty()).isTrue(); + } + } + """, + """ + import java.util.List; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(List items) { + assertThat(items).isEmpty(); + } + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyHasSizeFromIsEqualToAssertionTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyHasSizeFromIsEqualToAssertionTest.java index 08341990f..1f51fa353 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyHasSizeFromIsEqualToAssertionTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyHasSizeFromIsEqualToAssertionTest.java @@ -325,4 +325,39 @@ void testMethod(List list) { ) ); } + + @Test + void collectionOfClassDeclaredInTestFile() { + rewriteRun( + //language=java + java( + """ + import java.util.List; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(List items) { + assertThat(2).isEqualTo(items.size()); + } + } + """, + """ + import java.util.List; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(List items) { + assertThat(items).hasSize(2); + } + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestEveryItemToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestEveryItemToAssertJTest.java index f47f6dc0e..3ed21bc74 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestEveryItemToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestEveryItemToAssertJTest.java @@ -155,4 +155,41 @@ void testMethod() { ) ); } + + @Test + void collectionOfClassDeclaredInTestFile() { + rewriteRun( + //language=java + java( + """ + import java.util.List; + + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.everyItem; + import static org.hamcrest.Matchers.instanceOf; + + class MyTest { + static class Item {} + + void testMethod(List items) { + assertThat(items, everyItem(instanceOf(Item.class))); + } + } + """, + """ + import java.util.List; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(List items) { + assertThat(items).hasOnlyElementsOfType(Item.class); + } + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestHasItemMatcherToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestHasItemMatcherToAssertJTest.java index f00c72180..43476262c 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestHasItemMatcherToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestHasItemMatcherToAssertJTest.java @@ -155,4 +155,41 @@ void testMethod() { ) ); } + + @Test + void collectionOfClassDeclaredInTestFile() { + rewriteRun( + //language=java + java( + """ + import java.util.List; + + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.hasItem; + import static org.hamcrest.Matchers.instanceOf; + + class MyTest { + static class Item {} + + void testMethod(List items) { + assertThat(items, hasItem(instanceOf(Item.class))); + } + } + """, + """ + import java.util.List; + + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(List items) { + assertThat(items).hasAtLeastOneElementOfType(Item.class); + } + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestHasPropertyToAssertJTest.java b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestHasPropertyToAssertJTest.java index ef4995b7e..deccf9032 100644 --- a/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestHasPropertyToAssertJTest.java +++ b/src/test/java/org/openrewrite/java/testing/hamcrest/HamcrestHasPropertyToAssertJTest.java @@ -207,4 +207,36 @@ void testMethod() { ) ); } + + @Test + void classDeclaredInTestFile() { + rewriteRun( + //language=java + java( + """ + import static org.hamcrest.MatcherAssert.assertThat; + import static org.hamcrest.Matchers.hasProperty; + + class MyTest { + static class Item {} + + void testMethod(Item item) { + assertThat(item, hasProperty("class")); + } + } + """, + """ + import static org.assertj.core.api.Assertions.assertThat; + + class MyTest { + static class Item {} + + void testMethod(Item item) { + assertThat(item).hasFieldOrProperty("class"); + } + } + """ + ) + ); + } }