Class NameConventionForJunit4TestClassesCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    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>
     
    Since:
    1.13.0
    Author:
    Zuy Alexey
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      Violation message key.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Constructor Detail

      • NameConventionForJunit4TestClassesCheck

        public NameConventionForJunit4TestClassesCheck()
    • Method Detail

      • setExpectedClassNameRegex

        public void setExpectedClassNameRegex​(String expectedClassNameRegex)
        Sets regexp to match 'expected' class names for JUnit tests.
        Parameters:
        expectedClassNameRegex - regexp to match 'correct' JUnit test class names.
      • setClassAnnotationNameRegex

        public void setClassAnnotationNameRegex​(String annotationNameRegex)
        Sets class test annotation name regexp for JUnit tests.
        Parameters:
        annotationNameRegex - regexp to match annotations for unit test classes.
      • setMethodAnnotationNameRegex

        public void setMethodAnnotationNameRegex​(String annotationNameRegex)
        Sets method test annotation name regexp for JUnit tests.
        Parameters:
        annotationNameRegex - regexp to match annotations for unit test classes.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST classDefNode)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck