Skip to content

Commit f920ae1

Browse files
authored
Only replace assertNotEquals(false, <boolean>) when the second argument is a primitive boolean (#1220)
`AssertNotEqualsBooleanToAssertBoolean` rewrote `assertNotEquals(false, x)` to `assertTrue(x)` for any `x`. With a `Map<String, Object>`, `assertNotEquals(false, map.get("k"))` became `assertTrue(map.get("k"))`, which does not compile, and with a `Boolean` holding `null` a passing assertion became an unboxing `NullPointerException`. Require a primitive `boolean`, as `AssertEqualsBooleanToAssertBoolean` already does.
1 parent 300bfba commit f920ae1

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎src/main/java/org/openrewrite/java/testing/cleanup/AssertNotEqualsBooleanToAssertBoolean.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4444
@Override
4545
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
4646
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
47-
if (ASSERT_NOT_EQUALS.matches(mi) && isBooleanLiteral(mi)) {
47+
if (ASSERT_NOT_EQUALS.matches(mi) && isBooleanLiteral(mi) && JavaType.Primitive.Boolean == mi.getArguments().get(1).getType()) {
4848
StringBuilder sb = new StringBuilder();
4949
String assertMethod = Boolean.parseBoolean(((J.Literal) mi.getArguments().get(0)).getValueSource()) ?
5050
"assertFalse" : "assertTrue";

‎src/test/java/org/openrewrite/java/testing/cleanup/AssertNotEqualsBooleanToAssertBooleanTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,42 @@ void test() {
105105
)
106106
);
107107
}
108+
109+
@Test
110+
void noChangeForObjectArgument() {
111+
//language=java
112+
rewriteRun(
113+
java(
114+
"""
115+
import java.util.Map;
116+
117+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
118+
119+
class Test {
120+
void test(Map<String, Object> map) {
121+
assertNotEquals(false, map.get("k"));
122+
}
123+
}
124+
"""
125+
)
126+
);
127+
}
128+
129+
@Test
130+
void noChangeForBoxedBooleanArgument() {
131+
//language=java
132+
rewriteRun(
133+
java(
134+
"""
135+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
136+
137+
class Test {
138+
void test(Boolean b) {
139+
assertNotEquals(false, b);
140+
}
141+
}
142+
"""
143+
)
144+
);
145+
}
108146
}

0 commit comments

Comments
 (0)