Skip to content

Commit f194d5e

Browse files
authored
Collapse assertions after conversion (#805)
* Collapse Hamcrest assertions after conversion * Collapse Hamcrest assertions in single pass * Also assert JUnit assertions are collapsed after conversion
1 parent fedef4d commit f194d5e

File tree

4 files changed

+262
-140
lines changed

4 files changed

+262
-140
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ private String typeToIndicator(JavaType type) {
154154
type.toString().replaceAll("<.*>", "") : "java.lang.Object";
155155
return String.format("#{anyArray(%s)}", str);
156156
}
157-
String str = type instanceof JavaType.Primitive || type.toString().startsWith("java.") ?
158-
type.toString().replaceAll("<.*>", "") : "java.lang.Object";
159-
return String.format("#{any(%s)}", str);
157+
if (type instanceof JavaType.Primitive || type.toString().startsWith("java.")) {
158+
return "#{any()}";
159+
}
160+
return "#{any(java.lang.Object)}";
160161
}
161162
}
162163
}

src/test/java/org/openrewrite/java/testing/assertj/AssertJBestPracticesTest.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,124 @@ void testMethod(List<String> list) {
101101
);
102102
}
103103

104+
@Nested
105+
class CollapseAfterConversion {
106+
@Test
107+
void hamcrestToCollapsedAssertions() {
108+
rewriteRun(
109+
spec -> spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "hamcrest-3")),
110+
//language=java
111+
java(
112+
"""
113+
import java.util.List;
114+
115+
import static org.hamcrest.MatcherAssert.assertThat;
116+
import static org.hamcrest.Matchers.*;
117+
118+
class BiscuitTest {
119+
void biscuits() {
120+
List<String> biscuits = List.of("Ginger", "Chocolate", "Oatmeal");
121+
assertThat(biscuits, is(not(nullValue())));
122+
assertThat(biscuits, hasSize(3));
123+
assertThat(biscuits, hasItem("Chocolate"));
124+
}
125+
}
126+
""",
127+
"""
128+
import java.util.List;
129+
130+
import static org.assertj.core.api.Assertions.assertThat;
131+
132+
class BiscuitTest {
133+
void biscuits() {
134+
List<String> biscuits = List.of("Ginger", "Chocolate", "Oatmeal");
135+
assertThat(biscuits).isNotNull();
136+
assertThat(biscuits)
137+
.hasSize(3)
138+
.contains("Chocolate");
139+
}
140+
}
141+
"""
142+
)
143+
);
144+
}
145+
146+
@Test
147+
void junitCollapsedAssertions() {
148+
rewriteRun(
149+
spec -> spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5")),
150+
//language=java
151+
java(
152+
"""
153+
import java.util.List;
154+
155+
import static org.junit.jupiter.api.Assertions.*;
156+
157+
class BiscuitTest {
158+
void biscuits() {
159+
List<String> biscuits = List.of("Ginger", "Chocolate", "Oatmeal");
160+
assertNotNull(biscuits);
161+
assertEquals(3, biscuits.size());
162+
assertTrue(biscuits.contains("Chocolate"));
163+
}
164+
}
165+
""",
166+
"""
167+
import java.util.List;
168+
169+
import static org.assertj.core.api.Assertions.assertThat;
170+
171+
class BiscuitTest {
172+
void biscuits() {
173+
List<String> biscuits = List.of("Ginger", "Chocolate", "Oatmeal");
174+
assertThat(biscuits)
175+
.hasSize(3)
176+
.contains("Chocolate");
177+
}
178+
}
179+
"""
180+
)
181+
);
182+
}
183+
184+
@Test
185+
void assertjCollapsedAssertions() {
186+
rewriteRun(
187+
//language=java
188+
java(
189+
"""
190+
import java.util.List;
191+
192+
import static org.assertj.core.api.Assertions.assertThat;
193+
194+
class BiscuitTest {
195+
void biscuits() {
196+
List<String> biscuits = List.of("Ginger", "Chocolate", "Oatmeal");
197+
assertThat(biscuits).isNotNull();
198+
assertThat(biscuits).hasSize(3);
199+
assertThat(biscuits).contains("Chocolate");
200+
}
201+
}
202+
""",
203+
"""
204+
import java.util.List;
205+
206+
import static org.assertj.core.api.Assertions.assertThat;
207+
208+
class BiscuitTest {
209+
void biscuits() {
210+
List<String> biscuits = List.of("Ginger", "Chocolate", "Oatmeal");
211+
assertThat(biscuits)
212+
.hasSize(3)
213+
.contains("Chocolate");
214+
}
215+
}
216+
"""
217+
)
218+
);
219+
}
220+
}
221+
104222
/**
105223
* Chained AssertJ assertions should be simplified to the corresponding dedicated assertion, as
106224
* per <a

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.java.JavaParser;
2323
import org.openrewrite.test.RecipeSpec;
2424
import org.openrewrite.test.RewriteTest;
25+
import org.openrewrite.test.TypeValidation;
2526

2627
import static org.openrewrite.java.Assertions.java;
2728

@@ -385,7 +386,8 @@ void test() {
385386
@Test
386387
void containsInAnyOrderWithArray() {
387388
rewriteRun(
388-
spec -> spec.recipe(new HamcrestMatcherToAssertJ("containsInAnyOrder", "containsExactlyInAnyOrder", null)),
389+
spec -> spec.recipe(new HamcrestMatcherToAssertJ("containsInAnyOrder", "containsExactlyInAnyOrder", null))
390+
.afterTypeValidationOptions(TypeValidation.all().methodInvocations(false)),
389391
//language=java
390392
java(
391393
"""

0 commit comments

Comments
 (0)