1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Generated by Apache Maven Doxia Site Renderer 1.4 at 18 Feb 2015 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CodeNarc - CodeNarc - JUnit Rules</title>
<style type="text/css" media="all">
@import url("./css/maven-base.css");
@import url("./css/maven-theme.css");
@import url("./css/site.css");
</style>
<link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
<meta name="Date-Revision-yyyymmdd" content="20150218" />
<meta http-equiv="Content-Language" content="en" />
</head>
<body class="composite">
<div id="banner">
<a href="./" id="bannerLeft">
<img src="images/codenarc-logo.png" alt="CodeNarc" />
</a>
<a href="http://github.com/CodeNarc" id="bannerRight">
<img src="images/forkme_right_red_aa0000.png" alt="Fork me on GitHub" />
</a>
<div class="clear">
<hr/>
</div>
</div>
<div id="breadcrumbs">
<div class="xleft">
<span id="publishDate">Last Published: 18 Feb 2015</span>
| <span id="projectVersion">Version: 0.23</span>
</div>
<div class="xright">
</div>
<div class="clear">
<hr/>
</div>
</div>
<div id="leftColumn">
<div id="navcolumn">
<h5>General</h5>
<ul>
<li class="none">
<a href="index.html" title="Home">Home</a>
</li>
<li class="none">
<a href="https://sourceforge.net/project/showfiles.php?group_id=250145" class="externalLink" title="Downloads">Downloads</a>
</li>
<li class="none">
<a href="apidocs/index.html" title="Javadocs">Javadocs</a>
</li>
<li class="none">
<a href="http://sourceforge.net/mail/?group_id=250145" class="externalLink" title="Mailing Lists">Mailing Lists</a>
</li>
<li class="none">
<a href="http://sourceforge.net/tracker/?group_id=250145" class="externalLink" title="Bug Tracker">Bug Tracker</a>
</li>
<li class="none">
<a href="http://sourceforge.net/projects/codenarc" class="externalLink" title="SourceForge Project">SourceForge Project</a>
</li>
<li class="none">
<a href="http://github.com/CodeNarc" class="externalLink" title="GitHub Project">GitHub Project</a>
</li>
</ul>
<h5>Running</h5>
<ul>
<li class="none">
<a href="codenarc-ant-task.html" title="Ant Task Usage">Ant Task Usage</a>
</li>
<li class="none">
<a href="codenarc-command-line.html" title="Command-Line">Command-Line</a>
</li>
<li class="none">
<a href="codenarc-run-as-a-test.html" title="Run as a Test">Run as a Test</a>
</li>
<li class="none">
<a href="codenarc-other-tools-frameworks.html" title="Other Tools/Frameworks">Other Tools/Frameworks</a>
</li>
</ul>
<h5>Using</h5>
<ul>
<li class="none">
<a href="codenarc-creating-ruleset.html" title="Creating a RuleSet">Creating a RuleSet</a>
</li>
<li class="none">
<a href="codenarc-creating-rule.html" title="Creating a Rule">Creating a Rule</a>
</li>
<li class="none">
<a href="codenarc-configuring-rules.html" title="Configuring Rules">Configuring Rules</a>
</li>
<li class="none">
<a href="StarterRuleSet-AllRulesByCategory.groovy.txt" title="Starter RuleSet (All)">Starter RuleSet (All)</a>
</li>
</ul>
<h5>Report Types</h5>
<ul>
<li class="none">
<a href="codenarc-HtmlReportWriter.html" title="HTML Report">HTML Report</a>
</li>
<li class="none">
<a href="codenarc-XmlReportWriter.html" title="XML Report">XML Report</a>
</li>
<li class="none">
<a href="codenarc-TextReportWriter.html" title="Text and IDE Reports">Text and IDE Reports</a>
</li>
</ul>
<h5>Sample Reports</h5>
<ul>
<li class="none">
<a href="SampleCodeNarcHtmlReport.html" title="Sample HTML Report">Sample HTML Report</a>
</li>
<li class="none">
<a href="SampleCodeNarcXmlReport.xml" title="Sample XML Report">Sample XML Report</a>
</li>
</ul>
<h5>Rules</h5>
<ul>
<li class="none">
<a href="codenarc-rule-index.html" title="Rule Index">Rule Index</a>
</li>
<li class="none">
<a href="codenarc-rules-basic.html" title="Basic Rules">Basic Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-braces.html" title="Braces Rules">Braces Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-concurrency.html" title="Concurrency Rules">Concurrency Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-convention.html" title="Convention Rules">Convention Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-design.html" title="Design Rules">Design Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-dry.html" title="DRY Rules">DRY Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-enhanced.html" title="Enhanced Rules">Enhanced Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-exceptions.html" title="Exceptions Rules">Exceptions Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-formatting.html" title="Formatting Rules">Formatting Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-generic.html" title="Generic Rules">Generic Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-grails.html" title="Grails Rules">Grails Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-groovyism.html" title="Groovyism Rules">Groovyism Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-imports.html" title="Imports Rules">Imports Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-jdbc.html" title="JDBC Rules">JDBC Rules</a>
</li>
<li class="none">
<strong>JUnit Rules</strong>
</li>
<li class="none">
<a href="codenarc-rules-logging.html" title="Logging Rules">Logging Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-naming.html" title="Naming Rules">Naming Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-size.html" title="Size/Complexity Rules">Size/Complexity Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-security.html" title="Security Rules">Security Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-serialization.html" title="Serialization Rules">Serialization Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-unnecessary.html" title="Unnecessary Rules">Unnecessary Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-unused.html" title="Unused Rules">Unused Rules</a>
</li>
</ul>
<h5>Developing</h5>
<ul>
<li class="none">
<a href="codenarc-developer-guide.html" title="Developer Guide">Developer Guide</a>
</li>
</ul>
<h5>Project Documentation</h5>
<ul>
<li class="collapsed">
<a href="project-info.html" title="Project Information">Project Information</a>
</li>
<li class="collapsed">
<a href="project-reports.html" title="Project Reports">Project Reports</a>
</li>
</ul>
<a href="http://sourceforge.net" title="Hosted on SourceForge.net" class="poweredBy">
<img class="poweredBy" alt="Hosted on SourceForge.net" src="http://sflogo.sourceforge.net/sflogo.php?group_id=208647&type=2" />
</a>
<a href="http://maven.apache.org" title="Build with Maven 2" class="poweredBy">
<img class="poweredBy" alt="Build with Maven 2" src="images/logos/maven-feather.png" />
</a>
</div>
</div>
<div id="bodyColumn">
<div id="contentBox">
<div class="section">
<h2>JUnit Rules ("<i>rulesets/junit.xml</i>")<a name="JUnit_Rules_rulesetsjunit.xml"></a></h2><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<div class="section">
<h3><a name="ChainedTest">ChainedTest</a> Rule<a name="ChainedTest_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.13</i></p>
<p>A test method that invokes another test method is a chained test; the methods are dependent on one another. Tests should be isolated, and not be dependent on one another.</p>
<p>Example of violations:</p>
<div>
<pre> class MyTest extends GroovyTestCase {
public void testFoo() {
// violations, calls test method on self
5.times { testBar() }
5.times { this.testBar() }
// OK, no violation: one arg method is not actually a test method
5.times { testBar(it) }
}
private static void assertSomething() {
testBar() // violation, even if in helper method
this.testBar() // violation, even if in helper method
}
public void testBar() {
// ...
}
}
</pre></div></div>
<div class="section">
<h3><a name="CoupledTestCase">CoupledTestCase</a> Rule<a name="CoupledTestCase_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.13</i></p>
<p>This rule finds test cases that are coupled to other test cases, either by invoking static methods on another test case or by creating instances of another test case. If you require shared logic in test cases then extract that logic to a new class where it can properly be reused. Static references to methods on the current test class are ignored.</p>
<p>Example of violations:</p>
<div>
<pre> class MyTest extends GroovyTestCase {
public void testMethod() {
// violation, static method call to other test
MyOtherTest.helperMethod()
// violation, instantiation of another test class
new MyOtherTest()
// no violation; same class
def input = MyTest.getResourceAsStream('sample.txt')
}
}
</pre></div></div>
<div class="section">
<h3><a name="JUnitAssertAlwaysFails">JUnitAssertAlwaysFails</a> Rule<a name="JUnitAssertAlwaysFails_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Rule that checks for JUnit <tt>assert()</tt> method calls with constant or literal arguments such that the assertion always fails. This includes:</p>
<ul>
<li><tt>assertTrue(false)</tt></li>
<li><tt>assertTrue(0)</tt></li>
<li><tt>assertTrue('')</tt></li>
<li><tt>assertTrue([])</tt></li>
<li><tt>assertTrue([:])</tt></li>
<li><tt>assertFalse(true)</tt></li>
<li><tt>assertFalse('abc')</tt></li>
<li><tt>assertFalse(99)</tt></li>
<li><tt>assertFalse([123])</tt></li>
<li><tt>assertFalse([a:123)</tt></li>
<li><tt>assertNull(CONSTANT)</tt>.</li>
<li><tt>assertNull([])</tt>.</li>
<li><tt>assertNull([123])</tt>.</li>
<li><tt>assertNull([:])</tt>.</li>
<li><tt>assertNull([a:123])</tt>.</li></ul>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="JUnitAssertAlwaysSucceeds">JUnitAssertAlwaysSucceeds</a> Rule<a name="JUnitAssertAlwaysSucceeds_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Rule that checks for JUnit <tt>assert()</tt> method calls with constant arguments such that the assertion always succeeds. This includes:</p>
<ul>
<li><tt>assertTrue(true)</tt></li>
<li><tt>assertTrue(99)</tt></li>
<li><tt>assertTrue('abc')</tt></li>
<li><tt>assertTrue([123])</tt></li>
<li><tt>assertTrue([a:123])</tt></li>
<li><tt>assertFalse(false)</tt></li>
<li><tt>assertFalse('')</tt></li>
<li><tt>assertFalse(0)</tt></li>
<li><tt>assertFalse([])</tt></li>
<li><tt>assertFalse([:)</tt></li>
<li><tt>assertNull(null)</tt></li></ul>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="JUnitFailWithoutMessage">JUnitFailWithoutMessage</a> Rule<a name="JUnitFailWithoutMessage_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects JUnit calling the <tt>fail()</tt> method without an argument. For better error reporting you should always provide a message.</p></div>
<div class="section">
<h3><a name="JUnitLostTest">JUnitLostTest</a> Rule<a name="JUnitLostTest_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.18</i></p>
<p>This rule checks for classes that import JUnit 4 classes and contain a <tt>public</tt>, instance, <tt>void</tt>, no-arg method named <i>test</i>* that is not annotated with the JUnit 4 <tt>@Test</tt> annotation.</p>
<p>Note: This rule should be disabled for Grails 2.x projects, since the Grails test framework can use AST Transformations to automatically annotate test methods.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p>
<p>Example of violations:</p>
<div>
<pre> import org.junit.Test
class MyTestCase {
void testMe() { } // missing @Test annotation
}
</pre></div></div>
<div class="section">
<h3><a name="JUnitPublicField">JUnitPublicField</a> Rule<a name="JUnitPublicField_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.19</i></p>
<p>Checks for public fields on a JUnit test class. There is usually no reason to have a public field (even a constant) on a test class.</p>
<p>Fields within interfaces and fields annotated with @Rule are ignored.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p>
<p>Example of violations:</p>
<div>
<pre> import org.junit.Test
class MyTestCase {
public int count // violation
public static final MAX_VALUE = 1000 // violation
@Test
void testMe() { }
}
</pre></div></div>
<div class="section">
<h3><a name="JUnitPublicNonTestMethod">JUnitPublicNonTestMethod</a> Rule<a name="JUnitPublicNonTestMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Rule that checks if a JUnit test class contains public methods other than standard test methods, JUnit framework methods or methods with JUnit annotations.</p>
<p>The following public methods are ignored by this rule:</p>
<ul>
<li>Zero-argument methods with names starting with "test"</li>
<li>The <tt>setUp()</tt> and <tt>tearDown()</tt> methods</li>
<li>Methods annotated with <tt>@Test</tt></li>
<li>Methods annotated with <tt>@Before</tt> and <tt>@After</tt></li>
<li>Methods annotated with <tt>@BeforeClass</tt> and <tt>@AfterClass</tt></li>
<li>Methods annotated with <tt>@Override</tt> </li></ul>
<p>Public, non-test methods on a test class violate conventional usage of test classes, and they typically break encapsulation unnecessarily.</p>
<p>Public, non-test methods may also hide unintentional <i>'Lost Tests'</i>. For instance, the test method declaration may (unintentionally) include methods parameters, and thus be ignored by JUnit. Or the method may (unintentionally) not follow the "test.." naming convention and not have the @Test annotation, and thus be ignored by JUnit.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="JUnitPublicProperty">JUnitPublicProperty</a> Rule<a name="JUnitPublicProperty_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.21</i></p>
<p>Checks for public properties defined on JUnit test classes. There is typically no need to expose a public property (with public <i>getter</i> and <i>setter</i> methods) on a test class.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">ignorePropertyNames</td>
<td align="left">Specifies one or more (comma-separated) property names that should be ignored (i.e., that should not cause a rule violation). The names may optionally contain wildcards (*,?).</td>
<td align="left"><tt>null</tt></td></tr></table>
<p>Example of violations:</p>
<div>
<pre> import org.junit.Test
class MyTestCase {
static String id // violation
def helper // violation
String name // violation
@Test
void testMe() { }
}
</pre></div></div>
<div class="section">
<h3><a name="JUnitSetUpCallsSuper">JUnitSetUpCallsSuper</a> Rule<a name="JUnitSetUpCallsSuper_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Rule that checks that if the JUnit <tt>setUp</tt> method is defined, that it includes a call to <tt>super.setUp()</tt>.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="JUnitStyleAssertions">JUnitStyleAssertions</a> Rule<a name="JUnitStyleAssertions_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects calling JUnit style assertions like <tt>assertEquals</tt>, <tt>assertTrue</tt>, <tt>assertFalse</tt>, <tt>assertNull</tt>, <tt>assertNotNull</tt>. Groovy 1.7 ships with a feature called the "power assert", which is an assert statement with better error reporting. This is preferable to the JUnit assertions.</p></div>
<div class="section">
<h3><a name="JUnitTearDownCallsSuper">JUnitTearDownCallsSuper</a> Rule<a name="JUnitTearDownCallsSuper_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Rule that checks that if the JUnit <tt>tearDown</tt> method is defined, that it includes a call to <tt>super.tearDown()</tt>.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="JUnitTestMethodWithoutAssert">JUnitTestMethodWithoutAssert</a> Rule<a name="JUnitTestMethodWithoutAssert_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>This rule searches for test methods that do not contain assert statements. Either the test method is missing assert statements, which is an error, or the test method contains custom assert statements that do not follow a proper assert naming convention. Test methods are defined as public void methods that begin with the work test or have a @Test annotation. By default this rule applies to the default test class names, but this can be changed using the rule's applyToClassNames property. An assertion is defined as either using the <tt>assert</tt> keyword or invoking a method that starts with the work assert, like assertEquals, assertNull, or assertMyClassIsSimilar. Also, any method named <tt>should.*</tt> also counts as an assertion so that <tt>shouldFail</tt> methods do not trigger an assertion, any method that starts with <tt>fail>> counts as an assertion, and any method that starts with <<<verify</tt> counts as an assertion. Since version 0.23 CodeNarc has support for JUnit's ExpectedException.</p>
<p>What counts as an assertion method can be overridden using the assertMethodPatterns property of the rule. The default value is this comma separated list of regular expressions:</p>
<div>
<pre> String assertMethodPatterns = 'assert.*,should.*,fail.*,verify.*,expect.*'
</pre></div>
<p>If you'd like to add any method starting with 'ensure' to the ignores then you would set the value to this:</p>
<div>
<pre> 'assert.*,should.*,fail.*,verify.*,ensure.*'
</pre></div></div>
<div class="section">
<h3><a name="JUnitUnnecessarySetUp">JUnitUnnecessarySetUp</a> Rule<a name="JUnitUnnecessarySetUp_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Rule that checks checks for JUnit <tt>setUp()</tt> methods that contain only a call to <tt>super.setUp()</tt>. The method is then unnecessary.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p>
<p>Here is an example of a violation:</p>
<div>
<pre> class MyTest extends TestCase {
void setUp() { // violation
super.setUp()
}
}
</pre></div></div>
<div class="section">
<h3><a name="JUnitUnnecessaryTearDown">JUnitUnnecessaryTearDown</a> Rule<a name="JUnitUnnecessaryTearDown_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Rule that checks checks for JUnit <tt>tearDown()</tt> methods that contain only a call to <tt>super.tearDown()</tt>. The method is then unnecessary.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p>
<p>Here is an example of a violation:</p>
<div>
<pre> class MyTest extends TestCase {
void tearDown() { // violation
super.tearDown()
}
}
</pre></div></div>
<div class="section">
<h3><a name="JUnitUnnecessaryThrowsException">JUnitUnnecessaryThrowsException</a> Rule<a name="JUnitUnnecessaryThrowsException_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.18</i></p>
<p>Check for <tt>throws</tt> clauses on JUnit test methods. That is not necessary in Groovy.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p>
<p>Example of violations:</p>
<div>
<pre> @Test
void shouldDoStuff() throws Exception { } // violation
@BeforeClass void initialize() throws Exception { } // violation
@Before void setUp() throws RuntimeException { } // violation
@After void tearDown() throws Exception { } // violation
@AfterClass void cleanUp() throws Exception { } // violation
@Ignore void ignored() throws Exception { } // violation
class MyTest extends GroovyTestCase {
void test1() throws Exception { } // violation
public void test2() throws IOException { } // violation
}
</pre></div></div>
<div class="section">
<h3><a name="SpockIgnoreRestUsed">SpockIgnoreRestUsed</a> Rule<a name="SpockIgnoreRestUsed_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.14</i></p>
<p>If Spock's <tt>@IgnoreRest</tt> annotation appears on any method, all non-annotated test methods are not executed. This behaviour is almost always unintended. It's fine to use @IgnoreRest locally during development, but when committing code, it should be removed.</p>
<p>The <i>specificationClassNames</i> and <i>specificationSuperclassNames</i> properties determine which classes are considered Spock <i>Specification</i> classes.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">specificationClassNames</td>
<td align="left">Specifies one or more (comma-separated) class names that should be treated as Spock Specification classes. The class names may optionally contain wildcards (*,?), e.g. "*Spec".</td>
<td align="left"><tt>null</tt></td></tr>
<tr class="a">
<td align="left">specificationSuperclassNames</td>
<td align="left">Specifies one or more (comma-separated) class names that should be treated as Spock Specification superclasses. In other words, a class that extends a matching class name is considered a Spock Specification . The class names may optionally contain wildcards (*,?), e.g. "*Spec".</td>
<td align="left">"*Specification"</td></tr></table>
<p>Example of violations:</p>
<div>
<pre> public class MySpec extends spock.lang.Specification {
@spock.lang.IgnoreRest
def "my first feature"() {
expect: false
}
def "my second feature"() {
given: def a = 2
when: a *= 2
then: a == 4
}
}
</pre></div></div>
<div class="section">
<h3><a name="UnnecessaryFail">UnnecessaryFail</a> Rule<a name="UnnecessaryFail_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.13</i></p>
<p>In a unit test, catching an exception and immediately calling Assert.fail() is pointless and hides the stack trace. It is better to rethrow the exception or not catch the exception at all.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p>
<p>Example of violations:</p>
<div>
<pre> public void testSomething() {
try {
something()
} catch (Exception e) {
fail(e.message)
}
try {
something()
} catch (Exception e) {
fail()
}
}
</pre></div></div>
<div class="section">
<h3><a name="UseAssertEqualsInsteadOfAssertTrue">UseAssertEqualsInsteadOfAssertTrue</a> Rule<a name="UseAssertEqualsInsteadOfAssertTrue_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects JUnit assertions in object equality. These assertions should be made by more specific methods, like <tt>assertEquals</tt>.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="UseAssertFalseInsteadOfNegation">UseAssertFalseInsteadOfNegation</a> Rule<a name="UseAssertFalseInsteadOfNegation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>In unit tests, if a condition is expected to be false then there is no sense using <tt>assertTrue</tt> with the negation operator. For instance, <tt>assertTrue(!condition)</tt> can always be simplified to <tt>assertFalse(condition)</tt>.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="UseAssertTrueInsteadOfAssertEqualsRule">UseAssertTrueInsteadOfAssertEqualsRule</a> Rule<a name="UseAssertTrueInsteadOfAssertEqualsRule_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects JUnit calling <tt>assertEquals</tt> where the first parameter is a boolean. These assertions should be made by more specific methods, like <tt>assertTrue</tt> or <tt>assertFalse</tt>.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">checkAssertStatements</td>
<td align="left">If <tt>true</tt>, then also check assert statements, e.g. <tt>assert x == true</tt>.</td>
<td align="left"><tt>false</tt></td></tr></table>
<p>All of the following examples can be simplified to assertTrue or remove the true literal:</p>
<div>
<pre> assertEquals(true, foo())
assertEquals("message", true, foo())
assertEquals(foo(), true)
assertEquals("message", foo(), true)
assertEquals(false, foo())
assertEquals("message", false, foo())
assertEquals(foo(), false)
assertEquals("message", foo(), false)
assert true == foo() // violation only if checkAssertStatements == true
assert foo() == true : "message" // violation only if checkAssertStatements == true
assert false == foo() // violation only if checkAssertStatements == true
assert foo() == false : "message" // violation only if checkAssertStatements == true
</pre></div></div>
<div class="section">
<h3><a name="UseAssertTrueInsteadOfNegation">UseAssertTrueInsteadOfNegation</a> Rule<a name="UseAssertTrueInsteadOfNegation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>In unit tests, if a condition is expected to be true then there is no sense using <tt>assertFalse</tt> with the negation operator. For instance, <tt>assertFalse(!condition)</tt> can always be simplified to <tt>assertTrue(condition)</tt>.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="UseAssertNullInsteadOfAssertEquals">UseAssertNullInsteadOfAssertEquals</a> Rule<a name="UseAssertNullInsteadOfAssertEquals_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects JUnit calling <tt>assertEquals</tt> where the first or second parameter is <tt>null</tt>. These assertion should be made against the <tt>assertNull</tt> method instead.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div>
<div class="section">
<h3><a name="UseAssertSameInsteadOfAssertTrue">UseAssertSameInsteadOfAssertTrue</a> Rule<a name="UseAssertSameInsteadOfAssertTrue_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects JUnit calling <tt>assertTrue</tt> or <tt>assertFalse</tt> where the first or second parameter is an <tt>Object#is()</tt> call testing for reference equality. These assertion should be made against the <tt>assertSame</tt> or <tt>assertNotSame</tt> method instead.</p>
<p>This rule sets the default value of the <i>applyToClassNames</i> property to only match class names ending in 'Test', 'Tests' or 'TestCase'.</p></div></div>
</div>
</div>
<div class="clear">
<hr/>
</div>
<div id="footer">
<div class="xright">
Copyright © 2015.
All Rights Reserved.
</div>
<div class="clear">
<hr/>
</div>
</div>
</body>
</html>
|