简体   繁体   中英

how to deal with assertionError when writing custom sonar rules for java

I'm writing a custom sonar rule for java using java. I encountered an assertion error which can not be fixed easily. I'm sure that the source code is correct. But the test case can not be passed. I wonder what should I care about when using TDD process and how can I fix it.

public class logTCheckFile {
    private static Logger logger = Logger.getLogger(logTCheckFile.class);
    public void loggingWithID(String nonsense) throws myException{
        logger.error("errorID:20160801 this is an error");
        return;
    }

    public void loggingWithoutID(String nonsens){
        try{
            logger.error("this is an error");
        }catch(NullPointerException e){
            logger.error("what",e);
        }
        return;
    }

    public void specific(){
        logger.error("only the logger");
        try{
            logger.error("this is an error");
        }catch(NullPointerException e){
            logger.error("without an exception");
        }
        return;
    }
}

I'm testing the file above, I wrote a rule to test whether the exception, which is not thrown, is printed in the logger.

The message is AssertionError: Unexpected at [20] (here is a picture of the failure stack trace)

The code I wrote to check the file is as follows:

public class logTCheck extends  IssuableSubscriptionVisitor {


Logger log = Logger.getLogger(logTCheck.class);

@Override
public List<Kind> nodesToVisit() {
    return ImmutableList.of(Kind.METHOD);
}

@Override
public void visitNode(Tree tree){
    MethodTree method = (MethodTree) tree;
    if(method.throwsClauses().size()==0){
        log.info("this method does not have a throw clause");
        BlockTree bt = method.block();
        for(StatementTree st:bt.body()){
            if(st.is(Kind.TRY_STATEMENT)){
                TryStatementTree tst = (TryStatementTree) st;
                for(CatchTree ct:tst.catches()){
                    for(StatementTree state:ct.block().body()){
                        ExpressionStatementTree ex = (ExpressionStatementTree)state;
                        MethodInvocationTree mit = (MethodInvocationTree) ex.expression();
                        if(mit.arguments().size()!=2){
                            log.error(method.simpleName());
                            reportIssue(method.simpleName(), "you didn't print the exception in the log");
                        }
                    }
                }
            }
        }
    };
 }
}

The message is AssertionError: Unexpected at [20]

This means that line 20 of the test data contains a violation of the rule you are checking.

You need to tell the validator that this violation is intentionally there. An easy way to do this is to add a comment like this on the line right before the violation:

// Noncompliant@+1 {{the violation message}}

The @+1 means that the violation is on the next line. Adjust the number appropriately.

The comment must be at the beginning of the line, or there may be whitespace in front of the // .

The violation message enclosed within {{...}} is optional, but highly recommended. When doing TDD, an easy way to enter the correct message is to add something like {{x}} which will cause the test to fail, and then you can copy the message from the test output into the test file to fix it.

I finally found the problem from another answer from Michael. I didn't tell the tester where the issue should be. I should use the comment// Noncompliant to mark out the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM