Wednesday, September 28, 2011

PMD - JUnit Rules

The tenth installment of explanation of PMD rules covering JUnit rules.

JUnit Rules

These rules deal with different problems that can occur with JUnit tests.

JUnitStaticSuite

The suite() method in a JUnit test needs to be both public and static.

Example

import junit.framework.*;
public class Foo extends TestCase {
public void suite() {} // oops, should be static
private static void suite() {} // oops, should be public
}

JUnitSpelling

Some JUnit framework methods are easy to misspell.

Example

import junit.framework.*;
public class Foo extends TestCase {
public void setup() {} // oops, should be setUp
public void TearDown() {} // oops, should be tearDown
}

JUnitAssertionsShouldIncludeMessage

JUnit assertions should include a message - i.e., use the three argument version of assertEquals(), not the two argument version.

Example

public class Foo extends TestCase {
public void testSomething() {
  assertEquals("foo", "bar");
  // Use the form:
  // assertEquals("Foo does not equals bar", "foo", "bar");
  // instead
}
}

JUnitTestsShouldIncludeAssert

JUnit tests should include at least one assertion. This makes the tests more robust, and using assert with messages provide the developer a clearer idea of what the test does.

Example

public class Foo extends TestCase {
  public void testSomething() {
      Bar b = findBar();
  // This is better than having a NullPointerException
  // assertNotNull("bar not found", b);
  b.work();
  }
}

TestClassWithoutTestCases

Test classes end with the suffix Test. Having a non-test class with that name is not a good practice, since most people will assume it is a test case. Test classes have test methods named testXXX.

Example

//Consider changing the name of the class if it is not a test
//Consider adding test methods if it is a test
public class CarTest {
   public static void main(String[] args) {
    // do something
   }
   // code
}

UnnecessaryBooleanAssertion

A JUnit test assertion with a boolean literal is unnecessary since it always will eval to the same thing. Consider using flow control (in case of assertTrue(false) or similar) or simply removing statements like assertTrue(true) and assertFalse(false). If you just want a test to halt, use the fail method.

Example

public class SimpleTest extends TestCase {
public void testX() {
  // Why on earth would you write this?
  assertTrue(true);
}
}

UseAssertEqualsInsteadOfAssertTrue

This rule detects JUnit assertions in object equality. These assertions should be made by more specific methods, like assertEquals.

Example

public class FooTest extends TestCase {
void testCode() {
  Object a, b;
  assertTrue(a.equals(b)); // bad usage
  assertEquals("a should equals b", a, b); // good usage
}
}

UseAssertSameInsteadOfAssertTrue

This rule detects JUnit assertions in object references equality. These assertions should be made by more specific methods, like assertSame, assertNotSame.

Example

public class FooTest extends TestCase {
void testCode() {
  Object a, b;
  assertTrue(a==b); // bad usage
  assertSame(a, b);  // good usage
}
}

UseAssertNullInsteadOfAssertTrue

This rule detects JUnit assertions in object references equality. These assertions should be made by more specific methods, like assertNull, assertNotNull.

Example

public class FooTest extends TestCase {
  void testCode() {
   Object a = doSomething();
   assertTrue(a==null); // bad usage
   assertNull(a);  // good usage
   assertTrue(a != null); // bad usage
   assertNotNull(a);  // good usage
  }
}

SimplifyBooleanAssertion

Avoid negation in an assertTrue or assertFalse test. For example, rephrase: assertTrue(!expr); as: assertFalse(expr);

Example

public class SimpleTest extends TestCase {
public void testX() {
  assertTrue("not empty", !r.isEmpty()); // replace with assertFalse("not empty", r.isEmpty())
  assertFalse(!r.isEmpty()); // replace with assertTrue(r.isEmpty())
}
}

No comments: