Added new junit rules: JUnitTestContainsTooManyAsserts, UseAssertTrueInsteadOfAssertEquals
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6905 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
1 parent
5a4ee14831
commit
6ccb5a260a
5 files changed
+246
-1
No files matched your search
@@ -470,7 +470,8 @@ New Java rules:
|
||||
Design ruleset: LogicInversion,UseVarargs,FieldDeclarationsShouldBeAtStartOfClass
|
||||
Import ruleset: UnnecessaryFullyQualifiedName
|
||||
Naming ruleset: ShortClassName
|
||||
StrictException ruleset : AvoidThrowingNewInstanceOfSameException, AvoidCatchingGenericException
|
||||
StrictException ruleset: AvoidThrowingNewInstanceOfSameException, AvoidCatchingGenericException
|
||||
JUnit ruleset: JUnitTestContainsTooManyAsserts, UseAssertTrueInsteadOfAssertEquals
|
||||
|
||||
New Java ruleset:
|
||||
android.xml: new rules specific to the Android platform
|
||||
|
||||
@@ -13,6 +13,7 @@ public class JunitRulesTest extends SimpleAggregatorTst {
|
||||
addRule(RULESET, "JUnitAssertionsShouldIncludeMessage");
|
||||
addRule(RULESET, "JUnitSpelling");
|
||||
addRule(RULESET, "JUnitStaticSuite");
|
||||
addRule(RULESET, "JUnitTestContainsTooManyAsserts");
|
||||
addRule(RULESET, "JUnitTestsShouldIncludeAssert");
|
||||
addRule(RULESET, "SimplifyBooleanAssertion");
|
||||
addRule(RULESET, "TestClassWithoutTestCases");
|
||||
@@ -20,6 +21,7 @@ public class JunitRulesTest extends SimpleAggregatorTst {
|
||||
addRule(RULESET, "UseAssertEqualsInsteadOfAssertTrue");
|
||||
addRule(RULESET, "UseAssertNullInsteadOfAssertTrue");
|
||||
addRule(RULESET, "UseAssertSameInsteadOfAssertTrue");
|
||||
addRule(RULESET, "UseAssertTrueInsteadOfAssertEquals");
|
||||
}
|
||||
|
||||
public static junit.framework.Test suite() {
|
||||
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<test-data>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit 3 Test contains no assert
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
//negative on rule
|
||||
public void testMyCaseWithNoAssert() {
|
||||
boolean myVar = false;
|
||||
//no assert here
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit 4 Test contains no assert
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
//negative on rule
|
||||
@Test
|
||||
public void myCaseWithNoAssert() {
|
||||
boolean myVar = false;
|
||||
//no assert here
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit 3 Test contains one assert
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
//negative on rule
|
||||
public void testMyCaseWithOneAssert() {
|
||||
boolean myVar = false;
|
||||
assertFalse("should be false",myVar);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit 4 Test contains one assert
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
//negative on rule
|
||||
@Test
|
||||
public void myCaseWithOneAssert() {
|
||||
boolean myVar = false;
|
||||
assertFalse("should be false",myVar);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit 3 Test contains more than one assert
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
//positive on rule
|
||||
public void testMyCaseWithMoreAsserts() {
|
||||
boolean myVar = false;
|
||||
assertFalse("myVar should be false",myVar);
|
||||
assertEquals("should equals false", false, myVar);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit 4 Test contains more than one assert
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
//positive on rule
|
||||
@Test
|
||||
public void myCaseWithMoreAsserts() {
|
||||
boolean myVar = false;
|
||||
assertFalse("myVar should be false",myVar);
|
||||
assertEquals("should equals false", false, myVar);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit 3 Test contains more than one assert, but allowed
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<rule-property name="maximumAsserts">2</rule-property>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
//positive on rule
|
||||
public void testMyCaseWithMoreAsserts() {
|
||||
boolean myVar = false;
|
||||
assertFalse("myVar should be false",myVar);
|
||||
assertEquals("should equals false", false, myVar);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit 4 Test contains more than one assert, but allowed
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<rule-property name="maximumAsserts">2</rule-property>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
//positive on rule
|
||||
@Test
|
||||
public void myCaseWithMoreAsserts() {
|
||||
boolean myVar = false;
|
||||
assertFalse("myVar should be false",myVar);
|
||||
assertEquals("should equals false", false, myVar);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<test-data>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit Test contains assertEquals on other than boolean literal
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
public void testMyCase() {
|
||||
String myVar = "test";
|
||||
String myVar2 = "test";
|
||||
assertEquals(myVar, myVar2);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
JUnit Test contains assertEquals on boolean literal
|
||||
]]></description>
|
||||
<expected-problems>4</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
public void testMyCaseWithAssertEqualsOnBoolean() {
|
||||
boolean myVar = true;
|
||||
assertEquals("myVar is true", true, myVar);
|
||||
assertEquals("myVar is true", myVar, true);
|
||||
assertEquals("myVar is true", false, myVar);
|
||||
assertEquals("myVar is true", myVar, false);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
@@ -356,5 +356,79 @@ public class SimpleTest extends TestCase {
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
<rule name="JUnitTestContainsTooManyAsserts"
|
||||
language="java"
|
||||
since="5.0"
|
||||
message="JUnit tests should not contain more than {0} asserts."
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule">
|
||||
<description>
|
||||
JUnit tests should not contain too many asserts. Many asserts are indicative of a
|
||||
complex test, for which it is harder to verify correctness. Consider breaking the
|
||||
test scenario into multiple simpler test scenarios. Customize the maximum number
|
||||
of assertions used by this Rule to suit your needs.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="maximumAsserts" type="Integer" min="1" max="1000" description="Maximum number of Asserts in a test method" value="1"/>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//MethodDeclarator[(@Image[fn:matches(.,'^test')] or ../../Annotation/MarkerAnnotation/Name[@Image='Test']) and count(..//PrimaryPrefix/Name[@Image[fn:matches(.,'^assert')]]) > $maximumAsserts]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
// Ok
|
||||
public void testMyCaseWithOneAssert() {
|
||||
boolean myVar = false;
|
||||
assertFalse("should be false", myVar);
|
||||
}
|
||||
|
||||
// Bad, too many asserts (assuming max=1)
|
||||
public void testMyCaseWithMoreAsserts() {
|
||||
boolean myVar = false;
|
||||
assertFalse("myVar should be false", myVar);
|
||||
assertEquals("should equals false", false, myVar);
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
<rule name="UseAssertTrueInsteadOfAssertEquals"
|
||||
language="java"
|
||||
since="5.0"
|
||||
message="Use assertTrue(x)/assertFalse(x) instead of assertEquals(true, x)/assertEquals(false, x)."
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule">
|
||||
<description>
|
||||
When asserting a value is the same as a boolean literal, use assertTrue/assertFalse, instead of assertEquals.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimaryExpression[PrimaryPrefix/Name[@Image = 'assertEquals']][PrimarySuffix/Arguments/ArgumentList//Expression/PrimaryExpression/PrimaryPrefix/Literal/BooleanLiteral]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class MyTestCase extends TestCase {
|
||||
public void testMyCase() {
|
||||
boolean myVar = true;
|
||||
// Ok
|
||||
assertTrue("myVar is true", myVar);
|
||||
// Bad
|
||||
assertEquals("myVar is true", true, myVar);
|
||||
// Bad
|
||||
assertEquals("myVar is false", false, myVar);
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
</ruleset>
|
||||
Reference in new issue
Block a user