From 2fd4c0322fa70a3af81beaf950cb3e05d713720e Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 24 Oct 2026 12:47:34 +0200 Subject: [PATCH 1/2] Leave tests that call assumptions alone in `TestsShouldIncludeAssertions` A test such as `assumeTrue(isLinux()); service.start();` has no assertion, so the recipe wrapped its body in `assertDoesNotThrow(() -> {...})`. That catches the `TestAbortedException` thrown by a failed assumption and reports a test that should have been skipped as a failure. Calls to JUnit Jupiter `Assumptions`, JUnit 4 `Assume` and AssertJ `Assumptions` now make the recipe leave the test unchanged. --- .../cleanup/TestsShouldIncludeAssertions.java | 14 ++++- .../resources/META-INF/rewrite/recipes.csv | 2 +- .../TestsShouldIncludeAssertionsTest.java | 60 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java b/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java index 91d1ab18c..51bbbba79 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java @@ -60,6 +60,12 @@ public class TestsShouldIncludeAssertions extends Recipe { "reactor.test.StepVerifier" ); + private static final List ASSUMPTIONS = Arrays.asList( + new MethodMatcher("org.junit.jupiter.api.Assumptions *(..)"), + new MethodMatcher("org.junit.Assume *(..)"), + new MethodMatcher("org.assertj.core.api.Assumptions *(..)") + ); + @Option(displayName = "Additional assertions", description = "A comma delimited list of packages and/or classes that will be identified as assertions. I.E. a common assertion utility `org.foo.TestUtil`.", example = "org.foo.TestUtil, org.bar", @@ -69,7 +75,7 @@ public class TestsShouldIncludeAssertions extends Recipe { String displayName = "Include an assertion in tests"; - String description = "For tests not having any assertions, wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`."; + String description = "For tests not having any assertions, wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`. Tests that make an assumption are left unchanged, as wrapping them would report a skipped test as a failure."; Set tags = singleton("RSPEC-S2699"); @@ -159,7 +165,7 @@ private boolean methodHasAssertion(J.Block body) { @Override public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) { J.MethodInvocation mi = super.visitMethodInvocation(method, atomicBoolean); - if (isAssertion(mi)) { + if (isAssertion(mi) || isAssumption(mi)) { atomicBoolean.set(Boolean.TRUE); } return mi; @@ -214,6 +220,10 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Se return findMethodDeclarationVisitor.reduce(classDeclaration, new HashSet<>()); } + private boolean isAssumption(J.MethodInvocation methodInvocation) { + return ASSUMPTIONS.stream().anyMatch(matcher -> matcher.matches(methodInvocation)); + } + private boolean isAssertion(J.MethodInvocation methodInvocation) { if (methodInvocation.getMethodType() == null) { return false; diff --git a/src/main/resources/META-INF/rewrite/recipes.csv b/src/main/resources/META-INF/rewrite/recipes.csv index 88ea0b407..02819b2d2 100644 --- a/src/main/resources/META-INF/rewrite/recipes.csv +++ b/src/main/resources/META-INF/rewrite/recipes.csv @@ -105,7 +105,7 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.RemoveTestPrefix,Remove `test` prefix from JUnit 5 tests,"Remove `test` from methods with `@Test`, `@ParameterizedTest`, `@RepeatedTest` or `@TestFactory`. They no longer have to prefix test to be usable by JUnit 5.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.SimplifyTestThrows,Simplify `throws` statements of tests,Replace all thrown exception classes of test method signatures by `Exception`.,1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoid,Test methods should have void return type,"Test methods annotated with `@Test`, `@ParameterizedTest`, `@RepeatedTest`, `@TestTemplate` should have `void` return type. Non-void return types can cause test discovery issues, and warnings as of JUnit 5.13+. This recipe changes the return type to `void` and removes `return` statements.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,, -maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.TestsShouldIncludeAssertions,Include an assertion in tests,"For tests not having any assertions, wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,"[{""name"":""additionalAsserts"",""type"":""String"",""displayName"":""Additional assertions"",""description"":""A comma delimited list of packages and/or classes that will be identified as assertions. I.E. a common assertion utility `org.foo.TestUtil`."",""example"":""org.foo.TestUtil, org.bar""}]", +maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.TestsShouldIncludeAssertions,Include an assertion in tests,"For tests not having any assertions, wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`. Tests that make an assumption are left unchanged, as wrapping them would report a skipped test as a failure.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,"[{""name"":""additionalAsserts"",""type"":""String"",""displayName"":""Additional assertions"",""description"":""A comma delimited list of packages and/or classes that will be identified as assertions. I.E. a common assertion utility `org.foo.TestUtil`."",""example"":""org.foo.TestUtil, org.bar""}]", maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.TestsShouldNotBePublic,Remove `public` visibility of JUnit 5 tests,"Remove `public` and optionally `protected` modifiers from methods with `@Test`, `@ParameterizedTest`, `@RepeatedTest`, `@TestFactory`, `@BeforeEach`, `@AfterEach`, `@BeforeAll`, or `@AfterAll`. They no longer have to be public visibility to be usable by JUnit 5.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,"[{""name"":""removeProtectedModifiers"",""type"":""Boolean"",""displayName"":""Remove protected modifiers"",""description"":""Also remove protected modifiers from test methods"",""example"":""true""}]", maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.datafaker.JavaFakerToDataFaker,Migrate from Java Faker to Datafaker,Change imports and dependencies related to Java Faker to Datafaker replacements.,6,DataFaker,Testing,Java,Recipes for migrating from JavaFaker to [DataFaker](https://www.datafaker.net/).,,Basic building blocks for transforming Java code.,, maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.dbrider.ExecutionListenerToDbRiderAnnotation,Migrate the `DBRiderTestExecutionListener` to the `@DBRider` annotation,Migrate the `DBRiderTestExecutionListener` to the `@DBRider` annotation. This recipe is useful when migrating from JUnit 4 `dbrider-spring` to JUnit 5 `dbrider-junit5`.,1,DBRider,Testing,Java,Recipes for [DBRider](https://database-rider.github.io/database-rider/) database testing framework.,,Basic building blocks for transforming Java code.,, diff --git a/src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertionsTest.java b/src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertionsTest.java index 40e4a580b..89db31318 100644 --- a/src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertionsTest.java +++ b/src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertionsTest.java @@ -428,6 +428,66 @@ public void methodTest() { ); } + @Test + void ignoreJupiterAssumption() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assumptions.assumeTrue; + class AaTest { + @Test + void onLinux() { + assumeTrue(System.getProperty("os.name").contains("Linux")); + System.out.println("start"); + } + } + """ + ) + ); + } + + @Test + void ignoreJUnit4Assume() { + //language=java + rewriteRun( + java( + """ + import org.junit.Assume; + import org.junit.jupiter.api.Test; + class AaTest { + @Test + void onLinux() { + Assume.assumeTrue(System.getProperty("os.name").contains("Linux")); + System.out.println("start"); + } + } + """ + ) + ); + } + + @Test + void ignoreAssertJAssumption() { + //language=java + rewriteRun( + java( + """ + import org.junit.jupiter.api.Test; + import static org.assertj.core.api.Assumptions.assumeThat; + class AaTest { + @Test + void onLinux() { + assumeThat(System.getProperty("os.name")).contains("Linux"); + System.out.println("start"); + } + } + """ + ) + ); + } + @Test void ignoreEmpty() { //language=java From 96d176a9cb17ccf8544e9b867ea940aa0295ba95 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 24 Oct 2026 13:02:46 +0200 Subject: [PATCH 2/2] Drop the AssertJ `Assumptions` matcher already covered by the `org.assertj.core.api` assertion prefix --- .../java/testing/cleanup/TestsShouldIncludeAssertions.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java b/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java index 51bbbba79..9c5a835b8 100644 --- a/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java +++ b/src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldIncludeAssertions.java @@ -62,8 +62,7 @@ public class TestsShouldIncludeAssertions extends Recipe { private static final List ASSUMPTIONS = Arrays.asList( new MethodMatcher("org.junit.jupiter.api.Assumptions *(..)"), - new MethodMatcher("org.junit.Assume *(..)"), - new MethodMatcher("org.assertj.core.api.Assumptions *(..)") + new MethodMatcher("org.junit.Assume *(..)") ); @Option(displayName = "Additional assertions",