Java Unit Test JUnit
Rules available in this category:
- SetUp_should_invoke_super_setUp
- Avoid_invoking_fail_method_in_a_catch_block
- Always_include_at_least_one_test_method_in_each_test_class
- Ensure_correct_signature_for_suite_method
- Avoid_hard_coding_location_of_files
- Avoid_setting_up_test_cases_in_constructor
- Always_override_tearDown_method
- Always_override_setUp_method
- JUnit_classes_should_have_main_method
- JUnitTests_Should_Include_Assert
- JUnit_assert_methods_should_include_messages
- JUnit_suite_should_be_public_static
- JUnit_test_case_has_no_test_method
- Suspicious_JUnit_framework_method
- Avoid_unnecessary_boolean_assert_methods
- Use_assertEquals
- Use_assertSame
- Use_assertNull_assertNotNull
Rule 1: SetUp_should_invoke_super_setUp
Severity:
Medium
Rule:
setUp method should invoke super.setUp().
Reason:
setUp method should invoke super.setUp().
Usage Example:
package com.rule;
import junit.framework.TestCase;
public class SetUp_should_invoke_super_setUp_violation extends TestCase
{
// A connection to a database
private Database theDatabase;
public void setUp() //Violation
{
// Clear out the database
theDatabase.clear ();
}
}
Should be written as:
package com.rule;
import junit.framework.TestCase;
public class SetUp_should_invoke_super_setUp_correction extends TestCase
{
// A connection to a database
private Database theDatabase;
public void setUp()
{
super.setUp();//Correction
// Clear out the database
theDatabase.clear ();
}
}
Reference:
http://263.aka.org.cn/Magazine/Aka6/light/JUnit%20best%20practices.htm
Rule 2: Avoid_invoking_fail_method_in_a_catch_block
Severity:
Medium
Rule:
Explicitly calling the fail method from a catch block in a JUnit test method is unnecessary.
Reason:
Explicitly calling the fail method from a catch block in a JUnit test method is unnecessary.
Usage Example:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import junit.framework.*;
public class Test extends TestCase
{
public void testMethod()
{
try
{
FileInputStream data = new FileInputStream("c:/data.txt");
}
catch(FileNotFoundException e)
{
Assert.fail(); // VIOLATION
}
}
}
Should be written as:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import junit.framework.*;
public class Test extends TestCase
{
public void testMethod() throws FileNotFoundException
{
FileInputStream data = new FileInputStream("c:/data.txt"); // FIXED
}
}
Reference:
Not available.
Rule 3: Always_include_at_least_one_test_method_in_each_test_class
Severity:
Medium
Rule:
The test class has no purpose without a test method.
Reason:
The test class has no purpose without a test method.
Usage Example:
import junit.framework.TestCase;
public class Test extends TestCase // VIOLATION
{
public void method()
{
}
}
Should be written as:
import junit.framework.TestCase;
public class Test extends TestCase
{
public void testMethod() // FIXED
{
assertFalse(true);
}
}
Reference:
Not available.
Rule 4: Ensure_correct_signature_for_suite_method
Severity:
Medium
Rule:
If the suite method does not match the required signature, the test runner may be unable to use it.
Reason:
If the suite method does not match the required signature, the test runner may be unable to use it.
Usage Example:
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class Test1 extends TestCase
{
public static Test suitw() // VIOLATION
{
TestSuite suite= new TestSuite();
suite.addTest(new UPSS());
return suite;
}
}
class Test2 extends TestCase
{
public Test suite()// VIOLATION
{
TestSuite suite= new TestSuite();
suite.addTest(new UPSS_2());
return suite;
}
}
Should be written as:
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class Test1 extends TestCase
{
public static Test suite()// FIXED
{
TestSuite suite= new TestSuite();
suite.addTest(new UPSSFixed());
return suite;
}
}
class Test2 extends TestCase
{
public static Test suite()// FIXED
{
TestSuite suite= new TestSuite();
suite.addTest(new UPSS_2Fixed());
return suite;
}
}
Reference:
Not available.
Rule 5: Avoid_hard_coding_location_of_files
Severity:
Medium
Rule:
Do not load data from hard-coded locations on a filesystem.
Reason:
Do not load data from hard-coded locations on a filesystem.
Usage Example:
import junit.framework.*;
import java.io.*;
public class DoNotHardCode extends TestCase
{
public void setUp()
{
FileInputStream data =
new FileInputStream("c:\\MyFile.dat"); // VIOLATION
}
}
Should be written as:
import junit.framework.*;
import java.io.*;
public class DoNotHardCode extends TestCase
{
public void setUp()
{
InputStream data =
ClassLoader.getSystemClassLoader().getResourceAsString("MyFile.dat"); // FIXED
}
}
Reference:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit_p.html
Rule 6: Avoid_setting_up_test_cases_in_constructor
Severity:
Medium
Rule:
Avoid setting up test cases in constructor.
Reason:
Avoid setting up test cases in constructor.
Usage Example:
import junit.framework.*;
public class AvoidSettingUpTestCasesInConstructor extends TestCase
{
private String testCaseIdent;
public AvoidSettingUpTestCasesInConstructor ()
{
super ();
testCaseIdent = "Test1"; // VIOLATION
}
}
Should be written as:
import junit.framework.*;
public class AvoidSettingUpTestCasesInConstructor extends TestCase
{
private String testCaseIdent;
public AvoidSettingUpTestCasesInConstructor ()
{
super ();
}
public void setUp ()
{
testCaseIdent = "Test1"; // CORRECTION
}
}
Reference:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html
Rule 7: Always_override_tearDown_method
Severity:
Low
Rule:
It is preferrable to override the tearDown() method to clean up all open resources.
Reason:
It is preferrable to override the tearDown() method to clean up all open resources.
Usage Example:
import junit.framework.*;
public class AlwaysOverrideTearDownMethod extends TestCase // VIOLATION: missing 'tearDown ()' method
{
public AlwaysOverrideTearDownMethod (String n)
{
super(n);
}
public void setUp ()
{
//...
}
}
Should be written as:
import junit.framework.*;
public class AlwaysOverrideTearDownMethod extends TestCase
{
public AlwaysOverrideTearDownMethod (String n)
{
super(n);
}
public void setUp ()
{
//...
}
public void tearDown () // CORRECTION
{
// cleanup code goes here
}
}
Reference:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html
Rule 8: Always_override_setUp_method
Severity:
Low
Rule:
It is preferrable to override the setUp() method to perform initialization.
Reason:
It is preferrable to override the setUp() method to perform initialization.
Usage Example:
import junit.framework.*;
public class Always_override_setUp_method extends TestCase // VIOLATION: missing 'setUp ()' method
{
public Always_override_setUp_method (String s)
{
super (s);
// ... extra initialization code
}
public void tearDown () { }
}
Should be written as:
import junit.framework.*;
public class Always_override_setUp_method extends TestCase {
public Always_override_setUp_method (String s)
{
super(s);
}
public void setUp () // FIXED
{
// ... extra initialization code
}
public void tearDown() { }
}
Reference:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html
Rule 9: JUnit_classes_should_have_main_method
Severity:
Low
Rule:
Having a main method allows the developers to run individual tests in isolation.
Reason:
Having a main method allows the developers to run individual tests in isolation.
Usage Example:
import junit.framework.*;
public class JUnitClassesShouldHaveMainMethod extends TestCase
{
// VIOLATION : missing main method
}
Should be written as:
import junit.framework.*;
public class JUnitClassesShouldHaveMainMethod extends TestCase
{
public static void main(String[] args) // CORRECTION
{
}
}
Reference:
No references available.
Rule 10: JUnitTests_Should_Include_Assert
Severity:
High
Rule:
JUnitTests Should Include Assert
Reason:
JUnitTests Should Include Assert
Usage Example:
package com.rule;
public class JUnitTests_Should_Include_Assert extends TestCase
{
public void testAdd()
{
double result= fValue1 + fValue2;
assertTrue(result == 5);
}
public void testSub() //Violation
{
double result= fValue1 + fValue2;
}
}
Should be written as:
package com.rule;
public class JUnitTests_Should_Include_Assert extends TestCase
{
public void testAdd()
{
double result= fValue1 + fValue2;
assertTrue(result == 5);
}
public void testSub()
{
double result= fValue1 - fValue2;
assertTrue(result == 2); //Correction
}
}
Reference:
Reference Not Available.
Rule 11: JUnit_assert_methods_should_include_messages
Severity:
Medium
Rule:
JUnit assert methods should include messages
Reason:
JUnit assert methods should include messages
Usage Example:
package com.rule;
public class JUnit_assert_methods_should_include_messages
{
public void testEquals()
{
assertEquals(12, 12); // Violation.
}
}
Should be written as:
package com.rule;
public class JUnit_assert_methods_should_include_messages
{
public void testEquals()
{
assertEquals("Equals Failed",12, 12); //Correction.
}
}
Reference:
Reference not available.
Rule 12: JUnit_suite_should_be_public_static
Severity:
Medium
Rule:
JUnit suite() method should be declared public static.
Reason:
JUnit suite() method should be declared public static.
Usage Example:
package com.rule;
public class JUnit_suite_should_be_public_static_violation
{
private static Test suite() // Violation.
{
}
}
Should be written as:
package com.rule;
public class JUnit_suite_should_be_public_static_correction
{
public static Test suite() //Correction.
{
}
}
Reference:
Reference not available.
Rule 13: JUnit_test_case_has_no_test_method
Severity:
Medium
Rule:
JUnit test class has not implemented any test method.
Reason:
JUnit test class has not implemented any test method.
Usage Example:
package com.rule;
public class JUnit_test_case_has_no_test_method_violation // Violation.
{
protected void setUp()
{
// Some Initializing
}
public static Test suite()
{
}
}
Should be written as:
package com.rule;
public class JUnit_test_case_has_no_test_method_correction
{
protected void setUp()
{
// Some Initializing
}
public static Test suite()
{
testEquals();
}
public void testEquals() //Correction.
{
assertEquals("Equals Failed",12, 12);
}
}
Reference:
Reference not available.
Rule 14: Suspicious_JUnit_framework_method
Severity:
High
Rule:
Avoid suspicious JUnit framework methods.
Reason:
Avoid suspicious JUnit framework methods.
Usage Example:
package com.rule;
public class Suspicious_JUnit_framework_method_violation
{
protected void teardown() //Violation.
{
}
}
Should be written as:
package com.rule;
public class Suspicious_JUnit_framework_method_correction
{
protected void tearDown() //Correction.
{
}
}
Reference:
Reference not available.
Rule 15: Avoid_unnecessary_boolean_assert_methods
Severity:
Medium
Rule:
Avoid unnecessary boolean assert methods
Reason:
Avoid unnecessary boolean assert methods
Usage Example:
package com.rule;
public class Avoid_unnecessary_boolean_assert_methods_violation
{
public void testEquals()
{
assertTrue(false); // Violation.
}
}
Should be written as:
package com.rule;
public class Avoid_unnecessary_boolean_assert_methods_correction
{
public void testEquals()
{
fail(); //Correction.
}
}
Reference:
Reference not available.
Rule 16: Use_assertEquals
Severity:
Medium
Rule:
Use assertEquals instead of assertTrue.
Reason:
Use assertEquals instead of assertTrue.
Usage Example:
package com.rule;
public class Use_assertEquals_violation
{
public void testEquals()
{
Object a, b ;
assertTrue(a.equals(b)); // Violation.
}
}
Should be written as:
package com.rule;
public class Use_assertEquals_correction
{
public void testEquals()
{
Object a, b ;
assertEquals("a equals b", a, b); //Correction.
}
}
Reference:
Reference not available.
Rule 17: Use_assertSame
Severity:
Medium
Rule:
Use assertSame instead of assertTrue.
Reason:
Use assertSame instead of assertTrue.
Usage Example:
package com.rule;
public class Use_assertSame_violation
{
public void testEquals()
{
Object a, b ;
assertTrue(a == b); // Violation.
}
}
Should be written as:
package com.rule;
public class Use_assertSame_correction
{
public void testEquals()
{
Object a, b ;
assertSame("Message: a == b ",a, b); //Correction.
}
}
Reference:
Reference not available.
Rule 18: Use_assertNull_assertNotNull
Severity:
Medium
Rule:
Use assertNull and assertNotNull instead of assertTrue and assertFalse.
Reason:
Use assertNull and assertNotNull instead of assertTrue and assertFalse.
Usage Example:
public class Test
{
void testCode()
{
Object a = doSomething();
assertTrue(a==null); // VIOLATION
//...
assertTrue(a != null); // VIOLATION
}
}
Should be written as:
public class Test
{
void testCode()
{
Object a = doSomething();
assertNull(a); // FIXED
//...
assertNotNull(a); // FIXED
}
}
Reference:
Reference not available.