Skip to content

Commit c75ba77

Browse files
authored
Do not incorrectly strip method calls from assertion arguments (#918)
1 parent bf27539 commit c75ba77

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

src/main/java/org/openrewrite/java/testing/assertj/SimplifyChainedAssertJAssertion.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
import java.util.ArrayList;
3636
import java.util.List;
37-
import java.util.Objects;
3837
import java.util.Set;
3938

4039
import static java.util.Collections.singleton;
@@ -170,7 +169,7 @@ private String getStringTemplateAndAppendArguments(J.MethodInvocation assertThat
170169
}
171170

172171
// If either argument is empty, we choose which one to add to the arguments list, and optionally extract the select
173-
arguments.add(extractEitherArgument(assertThatArgumentIsEmpty, assertThatArgument, methodToReplaceArgument));
172+
arguments.add(assertThatArgumentIsEmpty ? methodToReplaceArgument : assertThatArgument);
174173

175174
// Special case for Path.of() assertions
176175
if ("java.nio.file.Path".equals(requiredType) && dedicatedAssertion.contains("Raw") &&
@@ -181,17 +180,6 @@ private String getStringTemplateAndAppendArguments(J.MethodInvocation assertThat
181180

182181
return "assertThat(#{any()}).%s(#{any()})";
183182
}
184-
185-
private Expression extractEitherArgument(boolean assertThatArgumentIsEmpty, Expression assertThatArgument, Expression methodToReplaceArgument) {
186-
if (assertThatArgumentIsEmpty) {
187-
return methodToReplaceArgument;
188-
}
189-
// Only on the assertThat argument do we possibly replace the argument with the select; such as list.size() -> list
190-
if (chainedAssertMatcher.matches(assertThatArgument)) {
191-
return Objects.requireNonNull(((J.MethodInvocation) assertThatArgument).getSelect());
192-
}
193-
return assertThatArgument;
194-
}
195183
};
196184
}
197185
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,55 @@ void biscuits() {
269269
}
270270
}
271271

272+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/496")
273+
@Test
274+
void assertTrueContainsWithMethodCallArgPreservesMethodCall() {
275+
rewriteRun(
276+
spec -> spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5")),
277+
//language=java
278+
java(
279+
"""
280+
import java.util.Locale;
281+
import java.util.Set;
282+
283+
import static org.junit.jupiter.api.Assertions.assertTrue;
284+
285+
class MyTest {
286+
static class UnderTest {
287+
Locale asLocale() {
288+
return Locale.US;
289+
}
290+
}
291+
292+
void test(Set<Locale> localeSet) {
293+
UnderTest underTest = new UnderTest();
294+
assertTrue(localeSet.contains(underTest.asLocale()));
295+
}
296+
}
297+
""",
298+
"""
299+
import java.util.Locale;
300+
import java.util.Set;
301+
302+
import static org.assertj.core.api.Assertions.assertThat;
303+
304+
class MyTest {
305+
static class UnderTest {
306+
Locale asLocale() {
307+
return Locale.US;
308+
}
309+
}
310+
311+
void test(Set<Locale> localeSet) {
312+
UnderTest underTest = new UnderTest();
313+
assertThat(localeSet).contains(underTest.asLocale());
314+
}
315+
}
316+
"""
317+
)
318+
);
319+
}
320+
272321
/**
273322
* Chained AssertJ assertions should be simplified to the corresponding dedicated assertion, as
274323
* per <a

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,34 @@ void testMethod(Map<String, ?> map) {
711711
);
712712
}
713713

714+
@Test
715+
void argumentMatchingChainedAssertionIsNotStripped() {
716+
rewriteRun(
717+
spec -> spec.recipe(new SimplifyChainedAssertJAssertion("equals", "isTrue", "isEqualTo", "java.lang.Object")),
718+
//language=java
719+
java(
720+
"""
721+
import static org.assertj.core.api.Assertions.assertThat;
722+
723+
class MyTest {
724+
void testMethod(Object obj1, Object obj2, Object obj3) {
725+
assertThat(obj1.equals(obj2.equals(obj3))).isTrue();
726+
}
727+
}
728+
""",
729+
"""
730+
import static org.assertj.core.api.Assertions.assertThat;
731+
732+
class MyTest {
733+
void testMethod(Object obj1, Object obj2, Object obj3) {
734+
assertThat(obj1).isEqualTo(obj2.equals(obj3));
735+
}
736+
}
737+
"""
738+
)
739+
);
740+
}
741+
714742
@Test
715743
void sizeIsEqualToLongLiteralIsNotConverted() {
716744
rewriteRun(

0 commit comments

Comments
 (0)