【问题标题】:how to deal with assertionError when writing custom sonar rules for java为java编写自定义声纳规则时如何处理assertionError
【发布时间】:2016-12-11 11:38:20
【问题描述】:

我正在使用 java 为 java 编写自定义声纳规则。我遇到了一个无法轻易修复的断言错误。我确定源代码是正确的。但是测试用例不能通过。我想知道在使用TDD过程时我应该注意什么以及如何解决它。

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;
    }
}

我在测试上面的文件,我写了一个规则来测试是否在logger中打印了没有抛出的异常。

消息是AssertionError: Unexpected at [20](这是失败堆栈跟踪的图片)

我写的检查文件的代码如下:

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");
                        }
                    }
                }
            }
        }
    };
 }
}

【问题讨论】:

  • 您可能需要展示一些代码来提供建议。
  • 请展示您用作测试的代码示例,并说明您在自定义规则中所做的工作。
  • 请提供与断言错误相关的消息或轨迹。

标签: java sonarqube rules


【解决方案1】:

我终于从迈克尔的另一个回答中找到了问题。我没有告诉测试人员问题应该出在哪里。我应该使用评论//不合规来标记问题。

【讨论】:

    【解决方案2】:

    消息是 AssertionError: Unexpected at [20]

    这意味着测试数据的第 20 行包含违反您正在检查的规则。

    您需要告诉验证器此违规行为是故意存在的。 一个简单的方法是在违规之前的行中添加这样的评论:

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

    @+1 表示违规在下一行。 适当调整数字。

    注释必须在行首, 或者//前面可能有空格。

    {{...}} 中包含的违规消息是可选的,但强烈推荐。 做TDD的时候, 输入正确消息的一种简单方法是添加类似{{x}} 的内容,这将导致测试失败, 然后您可以将测试输出中的消息复制到测试文件中进行修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多