Class RequireFailForTryCatchInJunitCheck

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

    public class RequireFailForTryCatchInJunitCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    Checks if a try/catch block has a fail assertion at the end of a try block in a JUnit test method.

    Rationale: Tests should not complete the try block naturally if they are expecting a failure. If the try completes normally the test will pass successfully and skip over any assertions in the catch block. If tests are not expecting exceptions, then they should remove the catch block and propagate the exception to the JUnit caller which will display the full exception to the user.

    A JUnit test method is identified by the annotations placed on it. It is only considered a JUnit method if it contains the annotation 'org.junit.Test'. This check doesn't examine methods called by a test method. It must contain the annotation. Failures are identified by a method call to either:

    An example of how to configure the check is:

     <module name="RequireFailForTryCatchInJunitCheck"/>
     

    which will cause a violation in the example below:

       @Test
       public void testMyCase() {
         try { // <-- violation here because try block misses fail assertion.
           verifySomeResult(); // <-- add e.g. 'Assert.fail()' after this last statement
                               //     of the try block to resolve the violation
         }
         catch (IllegalArgumentException ex) {
           assertEquals("expected exception message",
             "Some message that is expected", ex.getMessage());
         }
       }
     
    Since:
    1.25.0
    Author:
    Richard Veach, Sebastian Thomschke
    • 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
      void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)  
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        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

      • RequireFailForTryCatchInJunitCheck

        public RequireFailForTryCatchInJunitCheck()
    • Method Detail

      • 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
      • beginTree

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

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