Sunday, 20 March 2016

PMD Coding practices - JUnit


JUnit - rules provided by PMD

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

JUnitSpelling
Some JUnit framework methods are easy to misspell.

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

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.

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.

UnnecessaryBooleanAssertion
A JUnit test assertion with a boolean literal is unnecessary since it always will evaluate to the same thing.
Consider using flow control (in case of assertTrue(false) or similar) or simply removing statements like assertTrue(true) andassertFalse(false).
If you just want a test to halt after findingan error, use the fail() method and provide an indication message of why it did.

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

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

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

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

JUnitTestContainsTooManyAsserts
JUnit tests should not contain too many asserts. Many asserts are indicative of a complex test, for which it is harder to verify correctness.
Consider breaking the test scenario into multiple, shorter test scenarios.
Customize the maximum number of assertions used by this Rule to suit your needs.

UseAssertTrueInsteadOfAssertEquals
When asserting a value is the same as a boolean literal, use assertTrue/assertFalse, instead of assertEquals.


Migration (JUnit)

JUnit4TestShouldUseBeforeAnnotation
In JUnit 3, the setUp method was used to set up all data entities required in running tests.
JUnit 4 skips the setUp method and executes all methods annotated with @Before before all tests

JUnit4TestShouldUseAfterAnnotation
In JUnit 3, the tearDown method was used to clean up all data entities required in running tests.
JUnit 4 skips the tearDown method and executes all methods annotated with @After after running each test

JUnit4TestShouldUseTestAnnotation
In JUnit 3, the framework executed all methods which started with the word test as a unit test.
In JUnit 4, only methods annotated with the @Test annotation are executed.

JUnit4SuitesShouldUseSuiteAnnotation
In JUnit 3, test suites are indicated by the suite() method.
In JUnit 4, suites are indicated through the @RunWith(Suite.class) annotation.

JUnitUseExpected
In JUnit4, use the @Test(expected) annotation to denote tests that should throw exceptions.
 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.