diff --git a/src/main/java/org/openrewrite/java/testing/junit5/AddJupiterDependencies.java b/src/main/java/org/openrewrite/java/testing/junit5/AddJupiterDependencies.java index 889ec1a9d..0c9365021 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/AddJupiterDependencies.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/AddJupiterDependencies.java @@ -26,7 +26,6 @@ import org.openrewrite.java.dependencies.AddDependency; import org.openrewrite.maven.MavenIsoVisitor; import org.openrewrite.maven.tree.ResolvedDependency; -import org.openrewrite.maven.tree.Scope; import org.openrewrite.xml.tree.Xml; import java.util.List; @@ -64,14 +63,14 @@ public TreeVisitor getVisitor(AddDependency.Accumulator acc return new TreeVisitor() { @Override public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx, Cursor parent) { - if(!(tree instanceof SourceFile)) { + if (!(tree instanceof SourceFile)) { return tree; } SourceFile s = (SourceFile) tree; - if(gv.isAcceptable(s, ctx)) { + if (gv.isAcceptable(s, ctx)) { s = (SourceFile) gv.visitNonNull(s, ctx); } - if(mv.isAcceptable(s, ctx)) { + if (mv.isAcceptable(s, ctx)) { s = (SourceFile) mv.visitNonNull(s, ctx); } return s; @@ -81,7 +80,7 @@ public TreeVisitor getVisitor(AddDependency.Accumulator acc private static AddDependency addJupiterDependency() { return new AddDependency("org.junit.jupiter", "junit-jupiter", "5.x", null, - "org.junit..*", null, null, null, null, "test", + "org.junit..*", null, null, null, null, null, null, null, null, null); } @@ -93,16 +92,16 @@ private static class AddJupiterGradle extends GroovyIsoVisitor @Override public G.CompilationUnit visitCompilationUnit(G.CompilationUnit t, ExecutionContext ctx) { Optional maybeGp = t.getMarkers().findFirst(GradleProject.class); - if(!maybeGp.isPresent()) { + if (!maybeGp.isPresent()) { return t; } GradleProject gp = maybeGp.get(); GradleDependencyConfiguration trc = gp.getConfiguration("testRuntimeClasspath"); - if(trc == null) { + if (trc == null) { return t; } ResolvedDependency jupiterApi = trc.findResolvedDependency("org.junit.jupiter", "junit-jupiter-api"); - if(jupiterApi == null) { + if (jupiterApi == null) { t = (G.CompilationUnit) addJupiterDependency().getVisitor(acc) .visitNonNull(t, ctx); } @@ -115,11 +114,12 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit t, ExecutionCont @EqualsAndHashCode(callSuper = false) private static class AddJupiterMaven extends MavenIsoVisitor { AddDependency.Accumulator acc; + @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { Xml.Document d = document; - List jupiterApi = getResolutionResult().findDependencies("org.junit.jupiter", "junit-jupiter-api", Scope.Test); - if(jupiterApi.isEmpty()) { + List jupiterApi = getResolutionResult().findDependencies("org.junit.jupiter", "junit-jupiter-api", null); + if (jupiterApi.isEmpty()) { d = (Xml.Document) addJupiterDependency().getVisitor(acc) .visitNonNull(d, ctx); } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/AddJupiterDependenciesTest.java b/src/test/java/org/openrewrite/java/testing/junit5/AddJupiterDependenciesTest.java new file mode 100644 index 000000000..56a46dc7d --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddJupiterDependenciesTest.java @@ -0,0 +1,104 @@ +/* + * 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.junit5; + +import org.intellij.lang.annotations.Language; +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.Issue; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.openrewrite.java.Assertions.*; +import static org.openrewrite.maven.Assertions.pomXml; + +class AddJupiterDependenciesTest implements RewriteTest { + + @Language("java") + private static final String SOME_TEST = """ + import org.junit.jupiter.api.Test; + + class FooTest { + @Test + void bar() { + } + } + """; + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new AddJupiterDependencies()) + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api")); + } + + @DocumentExample + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/585") + void addToTestScope() { + rewriteRun( + mavenProject("project", + srcTestJava(java(SOME_TEST)), + pomXml( + //language=xml + """ + + 4.0.0 + org.example + project + 0.0.1 + + """, + spec -> spec.after(pom -> { + assertThat(pom) + .contains("junit-jupiter") + .contains("test"); + return pom; + }) + ) + ) + ); + } + + @Test + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/585") + void addToCompileScope() { + rewriteRun( + mavenProject("project", + srcMainJava(java(SOME_TEST)), + pomXml( + //language=xml + """ + + 4.0.0 + org.example + project + 0.0.1 + + """, + spec -> spec.after(pom -> { + assertThat(pom) + .contains("junit-jupiter") + .doesNotContain("test"); + return pom; + }) + ) + ) + ); + } +}