public class NameConventionForJunit4TestClassesCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
This check verifies the name of JUnit4 test class for compliance with user defined naming convention(by default Check expects test classes names matching ".+Test\\d*|.+Tests\\d*|Test.+|Tests.+|.+IT|.+ITs|.+TestCase\\d*|.+TestCases\\d*" regex).
Class is considered to be a test if its definition or one of its method definitions annotated with user defined annotations. By default Check looks for classes which contain methods annotated with "Test" or "org.junit.Test".
Check has following options:
"expectedClassNameRegex" - regular expression which matches expected test class names. If test class name does not matches this regex then Check will log violation. This option defaults to ".+Test\\d*|.+Tests\\d*|Test.+|Tests.+|.+IT|.+ITs|.+TestCase\\d*|.+TestCases\\d*".
"classAnnotationNameRegex" - regular expression which matches test annotation names on classes. If class annotated with matching annotation, it is considered to be a test. This option defaults to empty regex(one that matches nothing). If for example this option set to "RunWith", then class "SomeClass" is considered to be a test:
@RunWith(Parameterized.class)
class SomeClass
{
}
"methodAnnotationNameRegex" - regular expression which matches test annotation names on methods. If class contains a method annotated with matching annotation, it is considered to be a test. This option defaults to "Test|org.junit.Test". For example, if this option set to "Test", then class "SomeClass" is considered to be a test.
class SomeClass
{
@Test
void method() {
}
}
Annotation names must be specified exactly the same way it specified in code, thus if Check must match annotation with fully qualified name, corresponding options must contain qualified annotation name and vice versa. For example, if annotation regex is "org.junit.Test" Check will recognize "@ org.junit.Test" annotation and will skip "@Test" annotation and vice versa if annotation regex is "Test" Check will recognize "@ Test" annotation and skip "@org.junit.Test" annotation.
Following configuration will adjust Check to look for classes annotated with annotation "RunWith" or classes with methods annotated with "Test" and verify that classes names end with "Test" or "Tests".
<module name="NameConventionForJUnit4TestClassesCheck">
<property name="expectedClassNameRegex" value=".+Tests|.+Test"/>
<property name="classAnnotationNameRegex" value="RunWith"/>
<property name="methodAnnotationNameRegex" value="Test"/>
</module>
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
Violation message key.
|
| Constructor and Description |
|---|
NameConventionForJunit4TestClassesCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
setClassAnnotationNameRegex(String annotationNameRegex)
Sets class test annotation name regexp for JUnit tests.
|
void |
setExpectedClassNameRegex(String expectedClassNameRegex)
Sets regexp to match 'expected' class names for JUnit tests.
|
void |
setMethodAnnotationNameRegex(String annotationNameRegex)
Sets method test annotation name regexp for JUnit tests.
|
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST classDefNode) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY
public NameConventionForJunit4TestClassesCheck()
public void setExpectedClassNameRegex(String expectedClassNameRegex)
expectedClassNameRegex - regexp to match 'correct' JUnit test class names.public void setClassAnnotationNameRegex(String annotationNameRegex)
annotationNameRegex - regexp to match annotations for unit test classes.public void setMethodAnnotationNameRegex(String annotationNameRegex)
annotationNameRegex - regexp to match annotations for unit test classes.public int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST classDefNode)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.