Skip to content

Commit 33c97a2

Browse files
committed
Correctly replace Date and Instant hamcrest assertions to AssertJ
Fixes #526
1 parent e2bb0ae commit 33c97a2

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToAssertJ.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@
3737
@AllArgsConstructor
3838
public class HamcrestMatcherToAssertJ extends Recipe {
3939

40-
@Option(displayName = "Hamcrest Matcher",
40+
@Option(displayName = "Hamcrest matcher",
4141
description = "The Hamcrest `Matcher` to migrate to JUnit5.",
4242
example = "equalTo",
4343
required = false)
4444
@Nullable
4545
String matcher;
4646

47-
@Option(displayName = "AssertJ Assertion",
47+
@Option(displayName = "AssertJ assertion",
4848
description = "The AssertJ method to migrate to.",
4949
example = "isEqualTo",
5050
required = false)
5151
@Nullable
5252
String assertion;
5353

54-
@Option(displayName = "Argument Type",
54+
@Option(displayName = "Argument type",
5555
description = "The type of the argument to the Hamcrest `Matcher`.",
5656
example = "java.math.BigDecimal",
5757
required = false)

src/main/resources/META-INF/rewrite/hamcrest.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,40 @@ recipeList:
6767
matcher: comparesEqualTo
6868
assertion: isEqualByComparingTo
6969

70+
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
71+
matcher: lessThan
72+
assertion: isBefore
73+
argumentType: java.util.Date
74+
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
75+
matcher: lessThanOrEqualTo
76+
assertion: isBeforeOrEqualTo
77+
argumentType: java.util.Date
78+
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
79+
matcher: greaterThan
80+
assertion: isAfter
81+
argumentType: java.util.Date
82+
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
83+
matcher: greaterThanOrEqualTo
84+
assertion: isAfterOrEqualTo
85+
argumentType: java.util.Date
86+
87+
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
88+
matcher: lessThan
89+
assertion: isBefore
90+
argumentType: java.time.Instant
91+
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
92+
matcher: lessThanOrEqualTo
93+
assertion: isBeforeOrEqualTo
94+
argumentType: java.time.Instant
95+
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
96+
matcher: greaterThan
97+
assertion: isAfter
98+
argumentType: java.time.Instant
99+
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
100+
matcher: greaterThanOrEqualTo
101+
assertion: isAfterOrEqualTo
102+
argumentType: java.time.Instant
103+
70104
- org.openrewrite.java.testing.hamcrest.HamcrestMatcherToAssertJ:
71105
matcher: equalTo
72106
assertion: isEqualTo

src/test/java/org/openrewrite/java/testing/hamcrest/MigrateHamcrestToAssertJTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.params.ParameterizedTest;
2222
import org.junit.jupiter.params.provider.Arguments;
2323
import org.junit.jupiter.params.provider.MethodSource;
24+
import org.junit.jupiter.params.provider.ValueSource;
2425
import org.openrewrite.DocumentExample;
2526
import org.openrewrite.InMemoryExecutionContext;
2627
import org.openrewrite.Issue;
@@ -793,4 +794,47 @@ void bar(List<String> list) {
793794
)
794795
);
795796
}
797+
798+
@ParameterizedTest
799+
@ValueSource(
800+
strings = {
801+
"java.util.Date",
802+
"java.time.Instant"
803+
}
804+
)
805+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/526")
806+
void greaterThanOrEqualToDate(String type){
807+
rewriteRun(
808+
java(
809+
"""
810+
import static org.hamcrest.MatcherAssert.assertThat;
811+
import static org.hamcrest.Matchers.greaterThan;
812+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
813+
import static org.hamcrest.Matchers.lessThan;
814+
import static org.hamcrest.Matchers.lessThanOrEqualTo;
815+
816+
class Foo {
817+
void bar(%1$s type) {
818+
assertThat(type, lessThan(type));
819+
assertThat(type, lessThanOrEqualTo(type));
820+
assertThat(type, greaterThan(type));
821+
assertThat(type, greaterThanOrEqualTo(type));
822+
}
823+
}
824+
""".formatted(type),
825+
"""
826+
import static org.assertj.core.api.Assertions.assertThat;
827+
828+
class Foo {
829+
void bar(%1$s type) {
830+
assertThat(type).isBefore(type);
831+
assertThat(type).isBeforeOrEqualTo(type);
832+
assertThat(type).isAfter(type);
833+
assertThat(type).isAfterOrEqualTo(type);
834+
}
835+
}
836+
""".formatted(type)
837+
)
838+
);
839+
}
796840
}

0 commit comments

Comments
 (0)