在为java编写自定义sonar规则时如何处理assertionError
我正在使用java为java编写一个自定义声纳规则。我遇到了一个断言错误,无法轻松修复。我确信源代码是正确的。但测试用例无法通过。我想知道在使用TDD过程时我应该关心什么,以及如何修复它在为java编写自定义sonar规则时如何处理assertionError,java,sonarqube,rules,Java,Sonarqube,Rules,我正在使用java为java编写一个自定义声纳规则。我遇到了一个断言错误,无法轻松修复。我确信源代码是正确的。但测试用例无法通过。我想知道在使用TDD过程时我应该关心什么,以及如何修复它 public class logTCheckFile { private static Logger logger = Logger.getLogger(logTCheckFile.class); public void loggingWithID(String nonsense) throws
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;
}
}
我正在测试上面的文件,我编写了一个规则来测试日志中是否打印了未抛出的异常
消息是(这里是故障堆栈跟踪的图片)
我为检查该文件而编写的代码如下:
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");
}
}
}
}
}
};
}
}
公共类日志检查扩展IssuableSubscriptionVisitor{
Logger log=Logger.getLogger(logTCheck.class);
@凌驾
公共列表nodesToVisit(){
返回ImmutableList.of(种类、方法);
}
@凌驾
公共无效访问节点(树){
MethodTree方法=(MethodTree)树;
if(method.throwsClauses().size()==0){
info(“此方法没有throw子句”);
BlockTree bt=method.block();
for(语句树st:bt.body()){
if(st.is(Kind.TRY_语句)){
TryStatementTree tst=(TryStatementTree)st;
for(CatchTree ct:tst.catchs()){
for(语句树状态:ct.block().body()){
ExpressionStatementTree ex=(ExpressionStatementTree)状态;
MethodInvocationTree mit=(MethodInvocationTree)ex.expression();
if(mit.arguments().size()!=2){
log.error(方法.simpleName());
reportIssue(method.simpleName(),“您没有在日志中打印异常”);
}
}
}
}
}
};
}
}
我终于从Michael的另一个答案中找到了问题。我没有告诉测试人员问题应该在哪里。我应该使用注释//不合规来标记问题
该消息在[20]处为AssertionError:Unexpected
这意味着测试数据的第20行包含违反您正在检查的规则的内容
您需要告诉验证器这是故意的。
要做到这一点,一个简单的方法是在违规之前的行中添加如下注释:
// Noncompliant@+1 {{the violation message}}
@+1
表示违规在下一行。
适当调整数字
注释必须位于行的开头,
或者,/
前面可能有空格
{…}
中包含的违规消息是可选的,但强烈建议使用。
进行TDD时,
输入正确消息的一个简单方法是添加类似{{x}}
的内容,这将导致测试失败,
然后,您可以将测试输出中的消息复制到测试文件中进行修复。您可能需要显示一些代码以提供建议。请显示用作测试的代码示例,并解释您在自定义规则中执行的操作。请提供与断言错误相关联的消息或strack跟踪。