Files
gh-openrewrite-rewrite-docs…/skills/writing-openrewrite-recipes/references/recipes-testing-common.csv
2025-11-30 08:45:33 +08:00

13 KiB

1Fully Qualified Recipe NameRecipe NameDescription
2org.openrewrite.java.spring.test.SpringRulesToJUnitExtensionReplace `SpringClassRule` and `SpringMethodRule` with JUnit 5 `SpringExtension`Replace JUnit 4's `SpringClassRule` and `SpringMethodRule` with JUnit 5's `SpringExtension` or rely on an existing `@SpringBootTest`.
3org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5MigrationMigrate Spring Boot 2.x projects to JUnit 5 from JUnit 4This recipe will migrate a Spring Boot application's tests from JUnit 4 to JUnit 5. This spring-specific migration includes conversion of Spring Test runners to Spring Test extensions and awareness of the composable Spring Test annotations.
4org.openrewrite.java.testing.assertj.AdoptAssertJDurationAssertionsAdopt AssertJ Duration assertionsAdopt AssertJ `DurationAssert` assertions for more expressive messages.
5org.openrewrite.java.testing.assertj.CollapseConsecutiveAssertThatStatementsCollapse consecutive `assertThat` statementsCollapse consecutive `assertThat` statements into single `assertThat` chained statement. This recipe ignores `assertThat` statements that have method invocation as parameter.
6org.openrewrite.java.testing.assertj.IsEqualToIgnoringMillisToIsCloseToRecipeReplace `AbstractDateAssert#isEqualToIgnoringMillis(java.util.Date)` by `by isCloseTo(Date long)``isEqualToIgnoringMillis()` is deprecated in favor of `isCloseTo()`.
7org.openrewrite.java.testing.assertj.JUnitAssertArrayEqualsToAssertThatJUnit `assertArrayEquals` to assertJConvert JUnit-style `assertArrayEquals()` to AssertJ's `assertThat().contains()` equivalents.
8org.openrewrite.java.testing.assertj.JUnitAssertEqualsToAssertThatJUnit `assertEquals` to AssertJConvert JUnit-style `assertEquals()` to AssertJ's `assertThat().isEqualTo()`.
9org.openrewrite.java.testing.assertj.JUnitAssertFalseToAssertThatJUnit `assertFalse` to AssertJConvert JUnit-style `assertFalse()` to AssertJ's `assertThat().isFalse()`.
10org.openrewrite.java.testing.assertj.JUnitAssertInstanceOfToAssertThatJUnit `assertInstanceOf` to AssertJConvert JUnit-style `assertInstanceOf()` to AssertJ's `assertThat().isInstanceOf()`.
11org.openrewrite.java.testing.assertj.JUnitAssertNotEqualsToAssertThatJUnit `assertNotEquals` to AssertJConvert JUnit-style `assertNotEquals()` to AssertJ's `assertThat().isNotEqualTo()`.
12org.openrewrite.java.testing.assertj.JUnitAssertNotNullToAssertThatJUnit `assertNotNull` to AssertJConvert JUnit-style `assertNotNull()` to AssertJ's `assertThat().isNotNull()`.
13org.openrewrite.java.testing.assertj.JUnitAssertNullToAssertThatJUnit `assertNull` to AssertJConvert JUnit-style `assertNull()` to AssertJ's `assertThat().isNull()`.
14org.openrewrite.java.testing.assertj.JUnitAssertSameToAssertThatJUnit `assertSame` to AssertJConvert JUnit-style `assertSame()` to AssertJ's `assertThat().isSameAs()`.
15org.openrewrite.java.testing.assertj.JUnitAssertThrowsToAssertExceptionTypeJUnit AssertThrows to AssertJ exceptionTypeConvert `JUnit#AssertThrows` to `AssertJ#assertThatExceptionOfType` to allow for chained assertions on the thrown exception.
16org.openrewrite.java.testing.assertj.JUnitAssertTrueToAssertThatJUnit `assertTrue` to AssertJConvert JUnit-style `assertTrue()` to AssertJ's `assertThat().isTrue()`.
17org.openrewrite.java.testing.assertj.JUnitFailToAssertJFailJUnit fail to AssertJConvert JUnit-style `fail()` to AssertJ's `fail()`.
18org.openrewrite.java.testing.assertj.JUnitTryFailToAssertThatThrownByConvert try-catch-fail blocks to AssertJ's assertThatThrownByReplace try-catch blocks where the try block ends with a `fail()` statement and the catch block optionally contains assertions with AssertJ's `assertThatThrownBy()`.
19org.openrewrite.java.testing.assertj.SimplifyAssertJAssertionSimplify AssertJ assertions with literal argumentsSimplify AssertJ assertions by replacing them with more expressive dedicated assertions.
20org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertionSimplify AssertJ chained assertionsMany AssertJ chained assertions have dedicated assertions that function the same. It is best to use the dedicated assertions.
21org.openrewrite.java.testing.assertj.SimplifyHasSizeAssertionSimplify AssertJ assertions with `hasSize` argumentSimplify AssertJ assertions by replacing `hasSize` with `hasSameSizeAs` dedicated assertions.
22org.openrewrite.java.testing.assertj.SimplifyRedundantAssertJChainsSimplify redundant AssertJ assertion chainsRemoves redundant AssertJ assertions when chained methods already provide the same or stronger guarantees.
23org.openrewrite.java.testing.cleanup.AssertEqualsBooleanToAssertBooleanReplace JUnit `assertEquals(false <boolean>)` to `assertFalse(<boolean>)` / `assertTrue(<boolean>)`Using `assertFalse` or `assertTrue` is simpler and more clear.
24org.openrewrite.java.testing.cleanup.AssertEqualsNullToAssertNull`assertEquals(a null)` to `assertNull(a)`Using `assertNull(a)` is simpler and more clear.
25org.openrewrite.java.testing.cleanup.AssertFalseEqualsToAssertNotEqualsReplace JUnit `assertFalse(a.equals(b))` to `assertNotEquals(ab)`Using `assertNotEquals(ab)` is simpler and more clear.
26org.openrewrite.java.testing.cleanup.AssertFalseNegationToAssertTrueReplace JUnit `assertFalse(!<boolean>)` to `assertTrue(<boolean>)`Using `assertTrue` is simpler and more clear.
27org.openrewrite.java.testing.cleanup.AssertFalseNullToAssertNotNullReplace JUnit `assertFalse(a == null)` to `assertNotNull(a)`Using `assertNotNull(a)` is simpler and more clear.
28org.openrewrite.java.testing.cleanup.AssertLiteralBooleanRemovedRecipeRemove JUnit `assertTrue(true)` and `assertFalse(false)`These assertions are redundant and do not provide any value. They can be safely removed.
29org.openrewrite.java.testing.cleanup.AssertLiteralBooleanToFailRecipesReplace JUnit `assertTrue(false "reason")` and `assertFalse(true "reason")` with `fail("reason")`Using fail is more direct and clear.
30org.openrewrite.java.testing.cleanup.AssertNotEqualsBooleanToAssertBooleanReplace JUnit `assertNotEquals(false <boolean>)` to `assertFalse(<boolean>)` / `assertTrue(<boolean>)`Using `assertFalse` or `assertTrue` is simpler and more clear.
31org.openrewrite.java.testing.cleanup.AssertTrueComparisonToAssertEqualsJunit `assertTrue(a == b)` to `assertEquals(ab)`Using `assertEquals(ab)` is simpler and more clear.
32org.openrewrite.java.testing.cleanup.AssertTrueEqualsToAssertEqualsReplace JUnit `assertTrue(a.equals(b))` to `assertEquals(ab)`Using `assertEquals(ab)` is simpler and more clear.
33org.openrewrite.java.testing.cleanup.AssertTrueNegationToAssertFalseReplace JUnit `assertTrue(!<boolean>)` to `assertFalse(<boolean>)`Using `assertFalse` is simpler and more clear.
34org.openrewrite.java.testing.cleanup.AssertTrueNullToAssertNullReplace JUnit `assertTrue(a == null)` to `assertNull(a)`Using `assertNull(a)` is simpler and more clear.
35org.openrewrite.java.testing.cleanup.AssertionsArgumentOrderAssertion arguments should be passed in the correct orderAssertions such as `org.junit.Assert.assertEquals` expect the first argument to be the expected value and the second argument to be the actual value; for `org.testng.Assert` it’s the other way around. This recipe detects `J.Literal` `J.NewArray` and `java.util.Iterable` arguments swapping them if necessary so that the error messages won't be confusing.
36org.openrewrite.java.testing.cleanup.RemoveEmptyTestsRemove empty tests without commentsRemoves empty methods with a `@Test` annotation if the body does not have comments.
37org.openrewrite.java.testing.cleanup.RemoveTestPrefixRemove `test` prefix from JUnit 5 testsRemove `test` from methods with `@Test` `@ParameterizedTest` `@RepeatedTest` or `@TestFactory`. They no longer have to prefix test to be usable by JUnit 5.
38org.openrewrite.java.testing.cleanup.SimplifyTestThrowsSimplify `throws` statements of testsReplace all thrown exception classes of test method signatures by `Exception`.
39org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoidTest methods should have void return typeTest 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.
40org.openrewrite.java.testing.cleanup.TestsShouldIncludeAssertionsInclude an assertion in testsFor tests not having any assertions wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`.
41org.openrewrite.java.testing.dbrider.ExecutionListenerToDbRiderAnnotationMigrate the `DBRiderTestExecutionListener` to the `@DBRider` annotationMigrate the `DBRiderTestExecutionListener` to the `@DBRider` annotation. This recipe is useful when migrating from JUnit 4 `dbrider-spring` to JUnit 5 `dbrider-junit5`.
42org.openrewrite.java.testing.easymock.EasyMockVerifyToMockitoVerifyReplace EasyMock `verify` calls with Mockito `verify` callsReplace `EasyMock.verify(dependency)` with individual `Mockito.verify(dependency).method()` calls based on expected methods.
43org.openrewrite.java.testing.easymock.RemoveExtendsEasyMockSupportMigrate Test classes that extend `org.easymock.EasyMockSupport` to use MockitoModify test classes by removing extends EasyMockSupport and replacing EasyMock methods with Mockito equivalents.
44org.openrewrite.java.testing.hamcrest.AssertThatBooleanToAssertJMigrate Hamcrest `assertThat(boolean Matcher)` to AssertJReplace Hamcrest `assertThat(String boolean)` with AssertJ `assertThat(boolean).as(String).isTrue()`.
45org.openrewrite.java.testing.hamcrest.HamcrestInstanceOfToJUnit5Migrate from Hamcrest `instanceOf` matcher to JUnit 5Migrate from Hamcrest `instanceOf` and `isA` matcher to JUnit5 `assertInstanceOf` assertion.
46org.openrewrite.java.testing.hamcrest.HamcrestIsMatcherToAssertJMigrate Hamcrest `is(Object)` to AssertJMigrate Hamcrest `is(Object)` to AssertJ `Assertions.assertThat(..)`.
47org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJMigrate from Hamcrest `Matcher` to AssertJMigrate from Hamcrest `Matcher` to AssertJ assertions.
48org.openrewrite.java.testing.hamcrest.HamcrestMatcherToJUnit5Migrate from Hamcrest `Matcher` to JUnit 5Migrate from Hamcrest `Matcher` to JUnit 5 assertions.
49org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJMigrate Hamcrest `not(Matcher)` to AssertJMigrate from Hamcrest `not(Matcher)` to AssertJ assertions.
50org.openrewrite.java.testing.hamcrest.HamcrestOfMatchersToAssertJMigrate `anyOf` Hamcrest Matcher to AssertJMigrate the `anyOf` Hamcrest Matcher to AssertJ's `satisfiesAnyOf` assertion.
51org.openrewrite.java.testing.jmockit.JMockitAnnotatedArgumentToMockitoConvert JMockit `@Mocked` and `@Injectable` annotated argumentsConvert JMockit `@Mocked` and `@Injectable` annotated arguments into Mockito statements.
52org.openrewrite.java.testing.jmockit.JMockitBlockToMockitoRewrite JMockit Expectations NonStrictExpectations Verifications VerificationsInOrder FullVerificationsRewrites JMockit `Expectations NonStrictExpectations Verifications VerificationsInOrder FullVerifications` blocks to Mockito statements.
53org.openrewrite.java.testing.jmockit.JMockitMockUpToMockitoRewrite JMockit MockUp to Mockito statementsRewrites JMockit `MockUp` blocks to Mockito statements. This recipe will not rewrite private methods in MockUp.
54org.openrewrite.java.testing.junit5.AddMissingNestedJUnit 5 inner test classes should be annotated with `@Nested`Adds `@Nested` to inner classes that contain JUnit 5 tests.
55org.openrewrite.java.testing.junit5.AddMissingTestBeforeAfterAnnotationsAdd missing `@BeforeEach` `@AfterEach` `@Test` to overriding methodsAdds `@BeforeEach` `@AfterEach` `@Test` to methods overriding superclass methods if the annotations are present on the superclass method.
56org.openrewrite.java.testing.junit5.AddParameterizedTestAnnotationAdd missing `@ParameterizedTest` annotation when `@ValueSource` is used or replace `@Test` with `@ParameterizedTest`Add missing `@ParameterizedTest` annotation when `@ValueSource` is used or replace `@Test` with `@ParameterizedTest`.
57org.openrewrite.java.testing.junit5.AssertThrowsOnLastStatementApplies JUnit 5 `assertThrows` on last statement in lambda block onlyApplies JUnit 5 `assertThrows` on last statement in lambda block only. In rare cases may cause compilation errors if the lambda uses effectively non final variables. In some cases tests might fail if earlier statements in the lambda block throw exceptions.
58org.openrewrite.java.testing.junit5.AssertToAssertionsJUnit 4 `Assert` To JUnit Jupiter `Assertions`Change JUnit 4's `org.junit.Assert` into JUnit Jupiter's `org.junit.jupiter.api.Assertions`.
59org.openrewrite.java.testing.junit5.AssertTrueInstanceofToAssertInstanceOfassertTrue(x instanceof y) to assertInstanceOf(y.class x)Migration of JUnit4 (or potentially JUnit5) test case in form of assertTrue(x instanceof y) to assertInstanceOf(y.class x).
60org.openrewrite.java.testing.junit5.CleanupJUnitImportsCleanup JUnit importsRemoves unused `org.junit` import symbols.
61org.openrewrite.java.testing.junit5.EnvironmentVariablesMigrate JUnit 4 environmentVariables rule to JUnit 5 system stubs extensionReplaces usage of the JUnit 4 `@Rule EnvironmentVariables` with the JUnit 5-compatible `SystemStubsExtension` and `@SystemStub EnvironmentVariables` from the System Stubs library.