Class RequireFailForTryCatchInJunitCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.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:
- com.google.common.truth.Truth#assert_.fail, com.google.common.truth.Truth#assertWithMessage.fail
- junit.framework.Assert#fail
- org.assertj.core.api.Assertions#fail
- org.assertj.core.api.Assertions#failBecauseExceptionWasNotThrown
- org.junit.Assert#fail
- org.junit.jupiter.api.Assertions#fail
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
-
-
Constructor Summary
Constructors Constructor Description RequireFailForTryCatchInJunitCheck()
-
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
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
Violation message key.- See Also:
- Constant Field Values
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
beginTree
public void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)
- Overrides:
beginTree
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-