Merge branch 'fixUseAssertTrueInsteadOfAssertEqualsRule' of https://github.com/Monits/pmd into Monits-fixUseAssertTrueInsteadOfAssertEqualsRule
This commit is contained in:
@ -403,18 +403,25 @@ public class MyTestCase extends TestCase {
|
|||||||
<rule name="UseAssertTrueInsteadOfAssertEquals"
|
<rule name="UseAssertTrueInsteadOfAssertEquals"
|
||||||
language="java"
|
language="java"
|
||||||
since="5.0"
|
since="5.0"
|
||||||
message="Use assertTrue(x)/assertFalse(x) instead of assertEquals(true, x)/assertEquals(false, x)."
|
message="Use assertTrue(x)/assertFalse(x) instead of assertEquals(true, x)/assertEquals(false, x)
|
||||||
|
or assertEquals(Boolean.TRUE, x)/assertEquals(Boolean.FALSE, x)."
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||||
externalInfoUrl="${pmd.website.baseurl}/rules/java/junit.html#UseAssertTrueInsteadOfAssertEquals">
|
externalInfoUrl="${pmd.website.baseurl}/rules/java/junit.html#UseAssertTrueInsteadOfAssertEquals">
|
||||||
<description>
|
<description>
|
||||||
When asserting a value is the same as a boolean literal, use assertTrue/assertFalse, instead of assertEquals.
|
When asserting a value is the same as a literal or Boxed boolean, use assertTrue/assertFalse, instead of assertEquals.
|
||||||
</description>
|
</description>
|
||||||
<priority>3</priority>
|
<priority>3</priority>
|
||||||
<properties>
|
<properties>
|
||||||
<property name="xpath">
|
<property name="xpath">
|
||||||
<value>
|
<value>
|
||||||
<![CDATA[
|
<![CDATA[
|
||||||
//PrimaryExpression[PrimaryPrefix/Name[@Image = 'assertEquals']][PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral]
|
//PrimaryExpression[PrimaryPrefix/Name[@Image = 'assertEquals']]
|
||||||
|
[
|
||||||
|
PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral
|
||||||
|
or
|
||||||
|
PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix
|
||||||
|
/Name[(@Image = 'Boolean.TRUE' or @Image = 'Boolean.FALSE')]
|
||||||
|
]
|
||||||
]]>
|
]]>
|
||||||
</value>
|
</value>
|
||||||
</property>
|
</property>
|
||||||
@ -430,6 +437,10 @@ public class MyTestCase extends TestCase {
|
|||||||
assertEquals("myVar is true", true, myVar);
|
assertEquals("myVar is true", true, myVar);
|
||||||
// Bad
|
// Bad
|
||||||
assertEquals("myVar is false", false, myVar);
|
assertEquals("myVar is false", false, myVar);
|
||||||
|
// Bad
|
||||||
|
assertEquals("myVar is true", Boolean.TRUE, myVar);
|
||||||
|
// Bad
|
||||||
|
assertEquals("myVar is false", Boolean.FALSE, myVar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]]>
|
]]>
|
||||||
|
@ -48,6 +48,48 @@ public class TestWithAssertEquals {
|
|||||||
public String methodWithBooleanParam(boolean param) {
|
public String methodWithBooleanParam(boolean param) {
|
||||||
return "a String value";
|
return "a String value";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
<test-code>
|
||||||
|
<description>
|
||||||
|
<![CDATA[
|
||||||
|
JUnit Test contains assertEquals with Boxed booleans
|
||||||
|
]]>
|
||||||
|
</description>
|
||||||
|
<expected-problems>8</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
public void test() {
|
||||||
|
final boolean myVar = true;
|
||||||
|
assertEquals("myVar is true", Boolean.TRUE, myVar);
|
||||||
|
assertEquals("myVar is true", myVar, Boolean.TRUE);
|
||||||
|
assertEquals(Boolean.TRUE, myVar);
|
||||||
|
assertEquals(myVar, Boolean.TRUE);
|
||||||
|
assertEquals("myVar is false", Boolean.FALSE, myVar);
|
||||||
|
assertEquals("myVar is false", myVar, Boolean.FALSE);
|
||||||
|
assertEquals(myVar, Boolean.FALSE);
|
||||||
|
assertEquals(Boolean.FALSE, myVar);
|
||||||
|
assertTrue(myVar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
<test-code>
|
||||||
|
<description>
|
||||||
|
<![CDATA[
|
||||||
|
JUnit Test contains assertEquals with Boxed booleans as param
|
||||||
|
]]>
|
||||||
|
</description>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
public void test() {
|
||||||
|
assertEquals(methodWithBooleanParam(Boolean.TRUE), "a String value", "they should be equal!");
|
||||||
|
}
|
||||||
|
public String methodWithBooleanParam(Boolean param) {
|
||||||
|
return "a String value";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]]></code>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
Reference in New Issue
Block a user