Proposed Rule Name: AssertStatementInTest
Proposed Category: Best Practices
Description: Assert statements should be avoided in test code in favour of assertion classes like JUnit's Assert or Assertions. These helper methods provide better error messages and make test code more readable. They also are not disabled when tests are run without -ea.
To identify test classes, the rule should use AbstractJUnitTest if it is implemented in PMD 6, or TestFrameworkUtil if it is implemented in PMD 7.
Code Sample: This should include code, that should be flagged by the rule. If possible, the "correct" code
according to this new rule should also be demonstrated.
import org.junit.*;
class Foo {
@Test
void testFoo() {
String s = "abc";
assert s.substring(1).equals("bc"); // warning, assert statement in test!
Assert.assertEquals("bc", s.substring(1)); // better
}
}
Real world example: https://github.com/spring-projects/spring-framework/blob/v5.0.6.RELEASE/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java#L422
Possible Properties: None
Additional context:
See #3488
Proposed Rule Name: AssertStatementInTest
Proposed Category: Best Practices
Description: Assert statements should be avoided in test code in favour of assertion classes like JUnit's
AssertorAssertions. These helper methods provide better error messages and make test code more readable. They also are not disabled when tests are run without-ea.To identify test classes, the rule should use
AbstractJUnitTestif it is implemented in PMD 6, orTestFrameworkUtilif it is implemented in PMD 7.Code Sample: This should include code, that should be flagged by the rule. If possible, the "correct" code
according to this new rule should also be demonstrated.
Real world example: https://github.com/spring-projects/spring-framework/blob/v5.0.6.RELEASE/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java#L422
Possible Properties: None
Additional context:
See #3488