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
}

    
		

JUnitAssertionsShouldIncludeMessageRule

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

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
    }
  }