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.

This rule is defined by the following XPath expression:

                
  //MethodDeclaration[not(@Static='true') or not(@Public='true')]
   [MethodDeclarator/@Image='suite']
   [MethodDeclarator/FormalParameters/@ParameterCount=0]
                
            

Here's an example of code that would trigger this rule:

			
  
  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.

This rule is defined by the following XPath expression:

              
//MethodDeclarator[(not(@Image = 'setUp')
 and translate(@Image, 'SETuP', 'setUp') = 'setUp')
 or (not(@Image = 'tearDown')
 and translate(@Image, 'TEARdOWN', 'tearDown') = 'tearDown')]
 [FormalParameters[count(*) = 0]]
              
          

Here's an example of code that would trigger this rule:

			

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.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.junit.JUnitAssertionsShouldIncludeMessage

Here's an example of code that would trigger this rule:

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

JUnitTestsShouldIncludeAssert

JUnit assertions should include an assertion - This makes the tests more robust and assert() with messages provide the developer a clearer idea of what the test does.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.junit.JUnitTestsShouldContainAsserts

Here's an example of code that would trigger this rule:

			
    
    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.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.TestClassWithoutTestCases

Here's an example of code that would trigger this rule:

			

//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) {
   ...
   }

   // code
}

      
		

UnnecessaryBooleanAssertion

A (junit test) asserttion 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);

This rule is defined by the following XPath expression:

    
//StatementExpression
[
.//Name[@Image='assertTrue' or  @Image='assertFalse']
and
PrimaryExpression/PrimarySuffix/Arguments/ArgumentList
 /Expression/PrimaryExpression/PrimaryPrefix
  /Literal/BooleanLiteral
]

              

Here's an example of code that would trigger this rule:

			

public class SimpleTest extends TestCase {
    public void testX() {
        code ...
        // 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.

This rule is defined by the following XPath expression:

                

//PrimaryExpression[
    PrimaryPrefix/Name[@Image = 'assertTrue']
][
    PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Name
    [ends-with(@Image, '.equals')]
]

 
            

Here's an example of code that would trigger this rule:

			

public class FooTest extends TestCase {
    void testCode() {
        Object a, b;
        assertTrue(a.equals(b)); // bad usage
        assertEquals(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.

This rule is defined by the following XPath expression:

                

//PrimaryExpression [
    PrimaryPrefix/Name[@Image = 'assertTrue' or @Image = 'assertFalse']
]
[PrimarySuffix/Arguments/ArgumentList/Expression/EqualityExpression[
        @Image = '==' or  @Image = '!='
    ]
]

 
            

Here's an example of code that would trigger this rule:

			

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