|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.testing.junit; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.openrewrite.DocumentExample; |
| 20 | +import org.openrewrite.InMemoryExecutionContext; |
| 21 | +import org.openrewrite.java.JavaParser; |
| 22 | +import org.openrewrite.test.RecipeSpec; |
| 23 | +import org.openrewrite.test.RewriteTest; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.openrewrite.java.Assertions.java; |
| 27 | +import static org.openrewrite.java.Assertions.javaVersion; |
| 28 | +import static org.openrewrite.maven.Assertions.pomXml; |
| 29 | + |
| 30 | +class JUnit6BestPracticesTest implements RewriteTest { |
| 31 | + |
| 32 | + @Override |
| 33 | + public void defaults(RecipeSpec spec) { |
| 34 | + spec |
| 35 | + .parser(JavaParser.fromJavaVersion() |
| 36 | + .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5")) |
| 37 | + .recipeFromResources("org.openrewrite.java.testing.junit.JUnit6BestPractices"); |
| 38 | + } |
| 39 | + |
| 40 | + @DocumentExample |
| 41 | + @Test |
| 42 | + void junit5ToJunit6() { |
| 43 | + rewriteRun( |
| 44 | + java( |
| 45 | + """ |
| 46 | + import org.junit.jupiter.api.MethodOrderer; |
| 47 | + import org.junit.jupiter.api.TestMethodOrder; |
| 48 | + import org.junit.jupiter.api.Test; |
| 49 | +
|
| 50 | + @TestMethodOrder(MethodOrderer.Alphanumeric.class) |
| 51 | + public class MyTest { |
| 52 | + @Test |
| 53 | + public void test1() { |
| 54 | + } |
| 55 | +
|
| 56 | + @Test |
| 57 | + public void test2() { |
| 58 | + } |
| 59 | + } |
| 60 | + """, |
| 61 | + """ |
| 62 | + import org.junit.jupiter.api.MethodOrderer; |
| 63 | + import org.junit.jupiter.api.TestMethodOrder; |
| 64 | + import org.junit.jupiter.api.Test; |
| 65 | +
|
| 66 | + @TestMethodOrder(MethodOrderer.MethodName.class) |
| 67 | + class MyTest { |
| 68 | + @Test |
| 69 | + void test1() { |
| 70 | + } |
| 71 | +
|
| 72 | + @Test |
| 73 | + void test2() { |
| 74 | + } |
| 75 | + } |
| 76 | + """, |
| 77 | + spec -> spec.markers(javaVersion(17)) |
| 78 | + ), |
| 79 | + //language=xml |
| 80 | + pomXml( |
| 81 | + """ |
| 82 | + <project> |
| 83 | + <modelVersion>4.0.0</modelVersion> |
| 84 | + <groupId>com.example</groupId> |
| 85 | + <artifactId>example</artifactId> |
| 86 | + <version>1.0.0</version> |
| 87 | + <properties> |
| 88 | + <junit-jupiter.version>5.14.4</junit-jupiter.version> |
| 89 | + </properties> |
| 90 | + <dependencyManagement> |
| 91 | + <dependencies> |
| 92 | + <dependency> |
| 93 | + <groupId>org.junit</groupId> |
| 94 | + <artifactId>junit-bom</artifactId> |
| 95 | + <version>${junit-jupiter.version}</version> |
| 96 | + <type>pom</type> |
| 97 | + <scope>import</scope> |
| 98 | + </dependency> |
| 99 | + </dependencies> |
| 100 | + </dependencyManagement> |
| 101 | + </project> |
| 102 | + """, |
| 103 | + spec -> spec.after(actual -> { |
| 104 | + assertThat(actual) |
| 105 | + .containsPattern("<junit-jupiter\\.version>6\\.\\d+\\.\\d+</junit-jupiter\\.version>"); |
| 106 | + return actual; |
| 107 | + }) |
| 108 | + ) |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments